dongshou7903 2018-04-06 14:59
浏览 23
已采纳

如何加入字符串数组?

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');

  • 写回答

1条回答 默认 最新

  • dongxian7471 2018-04-06 15:31
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启