Hello All -
A little new to go and reading from a SQL DBMS and trying to scan into a struct.
I am trying to keep it dynamic in sending a query and scanning into the required struct.
The structs I am using for the entities Member, in this case, I would like to use to read from other DBMS as well such as MongoDB thus do not want to be dependent on sql.NllString.
I understand I may have to use a different library such as sqlx by jmoiron - but wanted to ask first if there is a way to just use the sql package?
...
While I have gotten the following code fragment to work:
rows, err := db.Query("SELECT * FROM member")
PanicIf(err)
cols, err := rows.Columns()
PanicIf(err)
defer rows.Close()
vals := make([]interface{}, len(cols))
for i, _ := range cols {
vals[i] = new(sql.RawBytes)
}
I am trying to get the following part in reading into the struct Member to work:
members := []Member{}
for rows.Next() {
mem := Member{}
err := rows.Scan(vals...) // This works but I cannot get it into the Memeber struct as tried below.
// I want the following to work somehow?
// err := rows.Scan(&mem.ID, &mem.Name, &mem.Description, &mem.Country, &mem.Region)
PanicIf(err)
members = append(members, mem)
Would there be a way to use rows.Scan to scan directly into the Member struct.
...
Member struct is defined as:
type Member struct {
ID int
Name string
Description string
Country string // do not want to use sql.NullString as I do not want to make my memeber struct SQL DBMS dependent.
Region string // do not want to use sql.NullString as I do not want to make my memeber struct SQL DBMS dependent.
}
...
Thank you for your help!