I had a devil of a time figuring out how to get go’s sql query results
into a json result set without having to define a structure around it. Perhaps it can help others.
The thing that confused me the most was the sql.Columns.ScanType() function, which I had convinced myself would be critical to the process. In the end, neither it, nor the reflect package defining its return type, was required.
Instead, I create colvals, a []inteface{} to satisfy rows.Scan(), and populate it with pointers to interfaces I had gotten from new(interface{}). Once
rows.Scan(colvals...) returns, the value at each interface pointer is an interface that wraps the correct type for the data.
Storing all the rows as a []map[string]interface{} allows me to return json ofall the results like an API might, but without having to model my schema in golang types. I’m really excited to be able to define APIS without a lot of data odeling!