You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Can you explain why In the book.go allBooks function all variables for database query rows are redeclared inside "for" loop?
What about memory allocation? Have you tested all possible database response scenarios?
As shown in this tutorial variables are declared outside the loop:
var (
id int
name string
)
rows, err := db.Query("select id, name from users where id = ?", 1)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&id, &name)
if err != nil {
log.Fatal(err)
}
log.Println(id, name)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
The text was updated successfully, but these errors were encountered:
Hi, nice work! I am curious:
Can you explain why In the book.go allBooks function all variables for database query rows are redeclared inside "for" loop?
What about memory allocation? Have you tested all possible database response scenarios?
As shown in this tutorial variables are declared outside the loop:
Fetching Data from the Database
http://go-database-sql.org/retrieving.html
var (
id int
name string
)
rows, err := db.Query("select id, name from users where id = ?", 1)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&id, &name)
if err != nil {
log.Fatal(err)
}
log.Println(id, name)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
The text was updated successfully, but these errors were encountered: