Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
safe_single_query.go
func query(db *sql.DB, name string) {
query := fmt.Sprint("select id, name, grade from students ")
where := "where "
params := interface{}{}
if name != "" {
params = append(params, name)
where += "name=$%d",len(params)"
}
query = query + where
_, err := db.Query(query, params...)
if err != nil {
fmt.Println(err.Error())
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment