Created
March 2, 2014 07:34
-
-
Save rolaveric/9303240 to your computer and use it in GitHub Desktop.
Example of using an empty interface slice to hold pointers to another slice in Go
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
cols, err := rows.Cols() | |
// rows.Scan wants '[]interface{}' as an argument, but we want the values | |
// loaded into a '[]string'. So we must copy the references into such a slice | |
// See http://code.google.com/p/go-wiki/wiki/InterfaceSlice for details | |
scanArgs := make([]interface{}, len(cols)) | |
values := make([]string, len(cols)) | |
for i := range values { | |
scanArgs[i] = &values[i] | |
} | |
if rows.Next() { | |
// Load the row data into scanArgs, which references values | |
err := rows.Scan(scanArgs...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment