有没有一种方法可以在golang中使用包database / sql获取列的类型?

Basically, without knowing before hand what the resulting structure of a query might be, I'd like to query the database, and return a structure like this (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 a way to get the Type for a Column using package database/sql in golang?

I'm suspecting that what I want to do is

  1. make an array of interface{} the size of Column(),
  2. then for each column determine it's type,
  3. then fill the array with a pointer to that type,
  4. and then pass the array to Scan()

Which is a little like this code example from sqlx, but without first knowing the Struct that the data would be populating.

Using database/sql? No (as far as I know).

But you can use this code for arbitrary queries. And json.Marshall() from the json package will use reflection to determine the right way to print a value, so you could have a structure like this:

type Column struct {
    ColumnName  string
    ColumnValue interface{}
    ColumnType  string
}

And then use reflect.TypeOf(someVariable).String() to get the type for ColumnType.