Last active
June 19, 2022 13:51
-
-
Save VinGarcia/dbc7949ae7bba45a793e7b1e51cc8aee to your computer and use it in GitHub Desktop.
Golang SQL: Problems with existing libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { ... } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { ... } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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