dsc862009 2015-06-08 16:43
浏览 127

GoLang字符串参数打印

I have a sql select statement like this example:

queryValues = make([]interface{}, 0, 5)

queryValues = append(Name, obj.Name)
queryValues = append(Age, obj.Age)
whereClause := "where name = $1 and age = $2"    

query := fmt.Sprintf("Select * from Table1  %s;", whereClause)

rows, err := dbConnection.Query(query, queryValues...)

I have several questions here. What is the ... after queryValues? How come when I look at the query being passed up to the db none of the $1 are actually being converted into there real values?

What print function can I run to mimic dbConnection.Query(query, queryValues...) so that I can see it before it is passed up?

Thanks in advance.

Josh

  • 写回答

1条回答 默认 最新

  • donglinyi4313 2015-06-08 16:59
    关注

    The answer to your first question is that Query is what's referred to as a variadic function https://golang.org/ref/spec#Function_types. It takes any number of parameters of the last type, and is handed to the function as a slice.

    queryValues... is the reverse of that. It is taking your slice and passing them as individual arguments to query. It's doing the same thing as if you had done:

    dbConnection.Query(query, queryValues[0], queryValues[1])
    

    https://golang.org/ref/spec#Passing_arguments_to_..._parameters

    In this particular case you don't need the []interface{}

    dbConnection(query, obj.Name, obj.Age)
    

    The reason your $1, $2 placeholders are not being converted is likely because you're using the wrong placeholders for the particular driver you're using. It's dependent on the driver itself. For instance the MySQL driver uses '?' as it's placeholder.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?