【问题标题】:How to join array of strings?如何加入字符串数组?
【发布时间】:2018-04-06 14:59:17
【问题描述】:

我有一个 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');

【问题讨论】:

标签: go buffalo


【解决方案1】:

通常,您需要的占位符数量等于您传入的切片长度

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

然后类似

tx.Where(query, sentence...) 

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

【讨论】:

    猜你喜欢
    • 2021-09-30
    • 2013-06-07
    • 2018-10-01
    • 2012-05-26
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 2017-02-03
    • 2010-09-08
    相关资源
    最近更新 更多