Skip to content

Instantly share code, notes, and snippets.

@rolaveric
Created March 2, 2014 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rolaveric/9303240 to your computer and use it in GitHub Desktop.
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
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