func(rs * Rows)扫描以处理数组字符串的列类型

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?

1个回答

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
    }
doufei5537
doufei5537 完成工作,谢谢将标记为答案:)
一年多之前 回复
dongxun3777
dongxun3777 否。只需按答案中所示调用函数。
一年多之前 回复
dongyuan7981
dongyuan7981 我需要更改原始的struct def吗?
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐