sql.Named
is a struct type in the Go programming language’s database/sql
package. It is used to pass named parameters to SQL queries or statements.
When using Named
, you can pass the parameters as a map of strings to interface{} values, where each key represents the name of a parameter and its corresponding value is the parameter value.
For example:
params := map[string]interface{}{
"id": 1,
"name": "John",
}
stmt, err := db.PrepareNamed("SELECT * FROM users WHERE id=:id AND name=:name")
if err != nil {
log.Fatal(err)
}
rows, err := stmt.Query(params)
In this example, we define two named parameters: id
and name
. We then create a map called params
with keys "id"
and "name"
, each with their respective values. Finally, we prepare a statement that uses these named parameters (id=:id AND name=:name
) and execute it by passing the params
map to the query method.