Created
November 5, 2013 20:56
-
-
Save icholy/7326135 to your computer and use it in GitHub Desktop.
(*sql.Rows).Scan() dynamically by column name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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