var id int var name string err := db.QueryRow("SELECT id, name FROM users WHERE id = ?", 1).Scan(&id, &name) if err != nil { // handle the error } fmt.Printf("User %d's name is %v\n", id, name)
type User struct { ID int `db:"id"` Name string `db:"name"` } rows, err := db.Query("SELECT id, name FROM users") if err != nil { // handle the error } defer rows.Close() var users []User for rows.Next() { var user User err := rows.Scan(&user.ID, &user.Name) if err != nil { // handle the error } users = append(users, user) }This example queries the database for all users and scans the result into a slice of `User` structs. It demonstrates how to use tags to map the struct fields to the columns of the query's result. The package library for the Row Scan function is the `database/sql` package in Go.