Skip to content

Instantly share code, notes, and snippets.

@VinGarcia
Last active June 19, 2022 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VinGarcia/dbc7949ae7bba45a793e7b1e51cc8aee to your computer and use it in GitHub Desktop.
Save VinGarcia/dbc7949ae7bba45a793e7b1e51cc8aee to your computer and use it in GitHub Desktop.
Golang SQL: Problems with existing libraries
rows, err := db.Query("...")
defer rows.Close()
if err != nil { ... }
for rows.Next() {
var user User
err = rows.Scan(...)
if err != nil { ... }
}
if rows.Err() != nil { ... }
rows, err := db.Query(`SELECT
id,
username,
age,
-- other attributes --
FROM users
WHERE ...`)
// ...
for rows.Next() {
var user User
err = rows.Scan(
&user.ID,
&user.Username,
&user.Age,
// other attributes
)
// ...
}
rows, err := db.Query("...")
if err != nil { ... }
defer func() {
err = rows.Close()
if err != nil { ... }
}()
for rows.Next() {
var user User
err = rows.Scan(...)
if err != nil { ... }
}
if rows.Err() != nil { ... }
type User struct{
ID int `ksql:"user_id"`
Name string `ksql:"name"`
Age string `ksql:"age"`
}
var users []User
err := db.Query(ctx, &users, `FROM users WHERE type = $1`, userType)
if err != nil { ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment