0

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?

0

1 Answer 1

5

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
    }
Sign up to request clarification or add additional context in comments.

2 Comments

would I need to change my original struct def?
No. Just call the function as shown in the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.