Back to course

Executing Queries and Scanning Rows

Go (Golang) for Cloud-Native Microservices

Querying Data

Use QueryRow for single results and Query for multiple rows.

go row := db.QueryRow("SELECT name FROM users WHERE id=$1", id) var name string err := row.Scan(&name)

For multiple rows, always close the rows iterator: go rows, _ := db.Query("SELECT name FROM users") defer rows.Close() for rows.Next() { // scan... }