Skip to content

Instantly share code, notes, and snippets.

@icholy
Created November 5, 2013 20:56
Show Gist options
  • Save icholy/7326135 to your computer and use it in GitHub Desktop.
Save icholy/7326135 to your computer and use it in GitHub Desktop.
(*sql.Rows).Scan() dynamically by column name.
// the target column name
name := "latitude"
// latitude value will be scanned into here
var latitude float64
// grab the column names
columns, _ := rows.Columns()
// columns will be scanned into this slice
dest := make([]interface{}, len(columns))
for i := 0; i < len(columns); i++ {
if columns[i] == name {
dest[i] = &latitude
} else {
dest[i] = new(string) // everything else gets scanned into a string
}
}
_ = rows.Scan(dest...)
fmt.Println("latitude:", latitude)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment