Golang Join array interface

余生长醉 提交于 2019-11-29 13:56:01

If you want to pass elements of a slice to a function with variadic parameter, you have to use ... to tell the compiler you want to pass all elements individually and not pass the slice value as a single argument, so simply do:

tx.Exec(sqlStr, vals...)

This is detailed in the spec: Passing arguments to ... parameters.

Tx.Exec() has the signature of:

func (tx *Tx) Exec(query string, args ...interface{}) (Result, error)

So you have to pass vals.... Also don't forget to check returned error, e.g.:

res, err := tx.Exec(sqlStr, vals...)
if err != nil {
    // handle error
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!