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

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

Variadic arguments in cassandra query with gocql

问题

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

queryExec("INSERT INTO USERS VALUES(?,?,?,?)", userId, emailId, mobileNo, gender)

func queryExec(query string, args ...interface{}) error {
    err := session.Query(query, args...).Exec()
    return err
}

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

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

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

queryExec("INSERT INTO USERS VALUES(?,?,?,?)", userId, emailId, mobileNo, gender)

func queryExec(query string, args ...interface{}) err{
err := session.query(query, args).Exec()
return err

}

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

gocql : expected 4 values send got 1

答案1

得分: 0

应该是这样的:

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

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

英文:

It should be

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:

确定