stmt, err := db.PrepareNamed(`SELECT * FROM users WHERE name=:name`) if err != nil { // handle error } var user User err = stmt.Get(&user, map[string]interface{}{"name": "Alice"}) if err != nil { // handle error } // user now contains the row matching 'name' parameter
stmt, err := db.PrepareNamed(`SELECT * FROM users WHERE age>:age`) if err != nil { // handle error } rows, err := stmt.Queryx(map[string]interface{}{"age": 18}) if err != nil { // handle error } defer rows.Close() for rows.Next() { var user User err := rows.StructScan(&user) if err != nil { // handle error } // do something with user object }These examples demonstrate how to use PrepareNamed to execute SQL queries with named parameters. The PrepareNamed method belongs to the sqlx package and extends the functionality of the standard Go database/sql package.