In the Go SQL docs they give an example here of a query that only returns 1 column (poor example in my opinion, at least return 2...)
age := 27
rows, err := db.Query("SELECT name FROM users WHERE age=?", age)
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
log.Fatal(err)
}
fmt.Printf("%s is %d\n", name, age)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
The docs state here that Scan copies the columns in the current row into the values pointed at by dest.
How does this work with a struct, lets say I have a struct
type User struct{
Name string
Age int
}
and I modify my query to SELECT name, age from users where age=?
How do I unpack *Rows into my struct? I did find this example, but it didn't deal with structs. I will be following Active Record pattern conventions so my structs will map to my database via snake case conversion.