var id int var name string row := db.QueryRow("SELECT id, name FROM users WHERE id = ?", 1) err := row.Scan(&id, &name) if err != nil { // handle error } // do something with id and name
rows, err := db.Query("SELECT id, name FROM users WHERE age > 18") if err != nil { // handle error } defer rows.Close() for rows.Next() { var id int var name string err := rows.Scan(&id, &name) if err != nil { // handle error } // do something with id and name }In this code example, we first use the `db.Query()` method to execute an SQL query to retrieve multiple rows of data, selecting for the `id` and `name` columns. If there is any error in retrieving data, we handle it appropriately. We then use a `for` loop to loop through all rows of data retrieved by the query. We then declare and initialize variables `id` and `name`. We then use the `Scan()` method to retrieve `id` and `name` values for each row of data. If there is any error, we handle it appropriately. Finally, we do something with the retrieved values. Package Library: The package library for using database/sql Row in Go is database/sql package.