Will
Will

Reputation: 8631

func (rs *Rows) Scan to handle a column type of array string

I have a column within my Postgres db for tags which is an array of strings.

I have it defined within a struct in my golang as:

type device struct {
    deviceID   string
    macAddress sql.NullString
    name       sql.NullString
    agentID    sql.NullString
    groupType  sql.NullString
    tags       []string

    normalized           bool
    normalizedName       string
    normalizedMacAddress string
}

When I run the scan on the rows as such:

            err = rows.Scan(&d.deviceID, &d.name, &d.tags, &d.macAddress, &d.agentID, &d.groupType)
        if err != nil {
            return nil, err
        }

It returns the following error:

"sql: Scan error on column index 2, name "tags": unsupported Scan...+55 more"

So what kinda of wrapper do I need for a string array in order to be an acceptable type?

Upvotes: 0

Views: 301

Answers (1)

Thundercat
Thundercat

Reputation: 120951

Use pq.Array when scanning an array:

    err = rows.Scan(&d.deviceID, &d.name, pq.Array(&d.tags), &d.macAddress, &d.agentID, &d.groupType)
    if err != nil {
        return nil, err
    }

Upvotes: 5

Related Questions