reflection - Is there a way to get the Type for a Column using package database/sql in golang? -
basically, without knowing before hand resulting structure of query might be, i'd query database, , return structure (json-y)
// rows [ // row 1 [ { columnname: "id", value: 1, type: int }, { columnname: "name", value: "batman", type: string }, ... ], // row 2 [ { columnname: "id", value: 2, type: int }, { columnname: "name", value: "superman", type: string }, ... ] ]
is there way type column using package database/sql in golang?
i'm suspecting want is
- make array of interface{} size of column(),
- then each column determine it's type,
- then fill array pointer type,
- and pass array scan()
which little code example sqlx, without first knowing struct data populating.
using database/sql? no (as far know).
but can use this code arbitrary queries. , json.marshall()
json package use reflection determine right way print value, have structure this:
type column struct { columnname string columnvalue interface{} columntype string }
and use reflect.typeof(somevariable).string()
type columntype.
Comments
Post a Comment