Kyle Banks

Golang: Store Query Result in a Map

Written by @kylewbanks on Oct 8, 2016.

Converting the results of a SQL query to a struct in Go is trivial, but sometimes you don’t know ahead of time exactly what columns and types you’re going to be retrieving, and a map may be better suited for storing the results.

Using Structs

First, here’s the standard way we can convert results to a struct:

rows, _ := db.Query("SELECT ...") // Note: Ignoring errors for brevity
for rows.Next() {
    s := myStruct{}
    if err := rows.Scan(&s); err != nil {
        return err
    }
    
    // Do something with 's'
}

Easy enough, but storing your query result in a map is a bit trickier.

Using Maps

Let’s say for example you’re working with a user’s database where you don’t know the schema ahead of time. You can’t write a struct to store the results, because you don’t know what columns and data types you’re going to be retrieving. What we want in this case is a map[string]interface{} where the key is the column name and the value could be any data type.

You might assume you can do the following:

rows, _ := db.Query("SELECT ...") // Note: Ignoring errors for brevity
for rows.Next() {
    m := make(map[string]interface{})
    
    // This WON'T WORK
    if err := rows.Scan(&m); err != nil {
        // ERROR: sql: expected X destination arguments in Scan, not 1
    }
}

Basically the SQL package is thinking that you expect a single column to be returned and for it to be a map[string]interface{} compatible type, which isn’t what we we’re trying to do.

Instead what we have to do in order to make this work is the following:

rows, _ := db.Query("SELECT ...") // Note: Ignoring errors for brevity
cols, _ := rows.Columns()

for rows.Next() {
    // Create a slice of interface{}'s to represent each column,
    // and a second slice to contain pointers to each item in the columns slice.
    columns := make([]interface{}, len(cols))
    columnPointers := make([]interface{}, len(cols))
    for i, _ := range columns {
        columnPointers[i] = &columns[i]
    }
    
    // Scan the result into the column pointers...
    if err := rows.Scan(columnPointers...); err != nil {
        return err
    }

    // Create our map, and retrieve the value for each column from the pointers slice,
    // storing it in the map with the name of the column as the key.
    m := make(map[string]interface{})
    for i, colName := range cols {
        val := columnPointers[i].(*interface{})
        m[colName] = *val
    }
    
    // Outputs: map[columnName:value columnName2:value2 columnName3:value3 ...] 
    fmt.Print(m)
}

So how does this work? Well first we query and get our rows as usual, but this time we use rows.Columns() to get a reference to all column names in the result.

Then for each row, we create a slice of interface{}’s called columns who’s length matches the number of columns. Next we create a second slice with the same length called columnPointers, but this time we iterate over each element in columns and store a pointer to the interface{} element in our columnPointers slice. This is necessary because the sql package requires pointers when scanning. So now we have two slices, one of interface{}s and one of pointers to the interface{}s.

Now we can scan the row into the slice of interface{} pointers (ie. columnPointers).

Finally, we create our map[string]interface{}, and iterate over the column names. For each column name (colName), we deference the interface{} pointer at the current loop index from the columnPointers slice, which references the value in the columns slice. We take this dereferenced value and store it in the map as the value, with the key being the column name.

Now we can use the map however we need, essentially allowing us to perform queries dynamically, without requiring knowledge of the schema we’re going to be querying when we write our code.

This may seem a little confusing at first, especially if you have no prior experience with pointers. If so, I’d recommend reading up on Pointers and trying the code out, inserting some debug logging to fully understand what’s happening during each step of the process.

Let me know if this post was helpful on Twitter @kylewbanks or down below!