I have a struct that looks like:
type inv struct {
ID int `json:"id"`
Name string `json:"name"`
}
I'm querying some data from the database(assuming there are no errors):
rows, err := db.Query("select id, name from inv_table")
Normally, I'd have to pull the data from the row by scanning
var i inv
for rows.Next() {
rows.Scan(&i.ID, &i.Name)
}
I think this might work (to be tested tomorrow):
var i inv
for rows.Next() {
var x []interface{} = [&i.ID, &i.Name]
rows.Scan(x... )
}
In actuality I have many more columns in the result set from the query.
rows.Scan(&i)
or at least:
rows.Scan(getExportedValuePointers(&i)... )
I suppose I could always write some sort of encoder, however, it seems to me that there should be something in the box already.
** I realize that I could always write some reflection code similar to the encode/xml or encode/json ... but I'm hoping that someone has already done it.
UPDATE: The following code works as expected but not as I want:
package main
import "fmt"
type inv struct {
A int
B string
}
func main() {
var i inv
fmt.Printf("hello
")
n, err := fmt.Sscan("1 c", &i.A, &i.B)
fmt.Printf("retval: %d %#v
", n, err)
fmt.Printf("retval: %d, %s, %d %#v
", i.A, i.B, n, err)
j := []interface{}{&i.A, &i.B}
k := []interface{}{i.A, i.B}
n, err = fmt.Sscan("2 d", j... )
fmt.Printf("retval: %d, %s, %d %#v
", i.A, i.B, n, err)
fmt.Printf("retval: %d, %s
", k... )
}