Hi. I created a library that does exactly that and it's much simpler to use compared to sqlx: https://github.com/georgysavva/scany On Tuesday, December 16, 2014 at 9:08:03 PM UTC+3, Monosij Dutta-Roy wrote: > > > 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! >
-- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9da4a12d-3d47-45d7-908b-d3123eca4f49o%40googlegroups.com.