0

I have an API where I send a parameter currentList: ["hey", "cool"]

type sentenceCreateReq struct {
    CurrentList []string `json:"currentList"`
}

func (usr *SentenceResource) Create(c buffalo.Context) error {
    req := sentenceCreateReq{}
    parseReqBody(c.Request(), &req)

    ...

    sentence := models.Sentence{}
    err := tx.Limit(1).Where("word NOT IN (?)", c.Params("currentList")).First(&sentence)

    ...
}

How would I be able to join the array of strings to fit my requirement?

The SQL statement I want to run is

SELECT * FROM sentences WHERE word NOT IN ('hey', 'cool');

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
bigpotato
  • 26,262
  • 56
  • 178
  • 334
  • 3
    Possible duplicates: [Go and IN clause in Postgres](https://stackoverflow.com/questions/38036752/go-and-in-clause-in-postgres/38037586#38037586); and [Golang MySQL querying undefined amount of args using IN operator](https://stackoverflow.com/questions/39223856/golang-mysql-querying-undefined-amount-of-args-using-in-operator/39223999#39223999). – icza Apr 06 '18 at 15:03

1 Answers1

1

Generally you will need a number of placeholders equal to the length of the slice you are passing in

like this, for example: https://play.golang.org/p/AZRTUsfKyH7

then something like

tx.Where(query, sentence...) 

which will pass in the slice as separate arguments to fill in the placeholders.

Slabgorb
  • 786
  • 6
  • 17