如何加入字符串数组?

时间:2018-04-06 14:59:18

标签: go buffalo

我有一个API,我发送参数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)

    ...
}

我如何能够加入字符串数组以满足我的要求?

我要运行的SQL语句是

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

1 个答案:

答案 0 :(得分:1)

通常,您需要一些等于您传递的切片长度的占位符

像这样,例如: https://play.golang.org/p/AZRTUsfKyH7

然后像

tx.Where(query, sentence...) 

将作为单独的参数传入切片以填充占位符。