How can I pass a function's variadic arguments to a SQL.db Exec function?
This code
func dbExecOne(db *sql.DB, cmd string, args ...interface{}) error {
if _, err := db.Exec(cmd, args); err != nil {
return fmt.Errorf("db execute[%s, %s] %q", cmd, args, err)
}
return nil
}
Produces this error when called. If this can't be done, is there an alternate way, perhaps using a lambda to capture the command and args?
dbExecOne(db, "insert into junk values($1, $2)", 99, "foo")
db execute[insert into junk values($1, $2), [%!s(int=99) foo]]
"sql: converting argument $1 type: unsupported type []interface {}, a slice of interface"
You can always spread a slice's elements over variadic arguments with the ...
operator, like this:
db.Exec(cmd, args...)
For more info see the language specification
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.