在使用gocql进行Cassandra查询时,可以使用可变参数。

huangapple go评论118阅读模式
英文:

Variadic arguments in cassandra query with gocql

问题

我想要创建一个通用函数,用于使用gocql客户端执行Cassandra查询,类似于:

  1. queryExec("INSERT INTO USERS VALUES(?,?,?,?)", userId, emailId, mobileNo, gender)
  2. func queryExec(query string, args ...interface{}) error {
  3. err := session.Query(query, args...).Exec()
  4. return err
  5. }

但是当我传递多个参数值时,它给我返回以下错误:

  1. gocql: 期望发送4个值,但只收到1
英文:

I want to make a generic function for performing cassandra queries using the gocql client, something like :

  1. queryExec("INSERT INTO USERS VALUES(?,?,?,?)", userId, emailId, mobileNo, gender)
  2. func queryExec(query string, args ...interface{}) err{
  3. err := session.query(query, args).Exec()
  4. return err

}

but when I pass it multiple argument values, it gives me the following error :

  1. gocql : expected 4 values send got 1

答案1

得分: 0

应该是这样的:

  1. err := session.query(query, args...).Exec()

没有省略号,query接收到一个包含所有参数的切片。

英文:

It should be

  1. err := session.query(query, args...).Exec()

Without the ellipsis, query receives a slice containing all the args.

huangapple
  • 本文由 发表于 2017年3月23日 14:50:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/42969097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定