The error sql: no rows in result set occurs because your query returned zero rows, but you attempted to scan the result into a single variable using Row().Scan(). Use Query() to handle zero or multiple rows, or check err before scanning if you expect exactly one row.
var id int
err := db.QueryRow("SELECT id FROM users WHERE name = $1", "Alice").Scan(&id)
if err == sql.ErrNoRows {
// Handle the case where no user was found
log.Println("User not found")
return
}
if err != nil {
// Handle other database errors
log.Fatal(err)
}
// Use id here