Skip to content

Instantly share code, notes, and snippets.

@YanhaoYang
Created April 28, 2017 08:40
Show Gist options
  • Save YanhaoYang/77302b28d01bb2f09379c51ecfc51dab to your computer and use it in GitHub Desktop.
Save YanhaoYang/77302b28d01bb2f09379c51ecfc51dab to your computer and use it in GitHub Desktop.
SQL's IN query in Golang
func updateAll(ids []string) {
var params []string
var values []interface{}
for k, v := range ids {
params = append(params, fmt.Sprintf("$%d", k+1))
values = append(values, v)
}
db := viper.Get("db").(*gorp.DbMap)
sql := "UPDATE stores SET active = false WHERE id IN (" + strings.Join(params, ",") + ")"
rs, err := db.Exec(sql, values...)
if err != nil {
panic(err)
}
rows, err := rs.RowsAffected()
if err != nil {
panic(err)
}
fmt.Printf("%d rows updated.\n", rows)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment