dtol41388 2014-06-30 23:07
浏览 271
已采纳

扫描数据库行时出现错误“无法将类型[] uint8的值分配给类型[] string”

I am using postgresql as my backend database.

Tried to scan a field languagespoken which is an array of text

    var user userprofile

row := core.db.QueryRow("SELECT languagespoken FROM \"user\" WHERE id = $1",userId)

err := row.Scan(&user.Languages)

if err != nil {
    return user,err
}

My structure looks like this in golang

   type userprofile struct {
    Languages []string `json:languages`
    }

But getting the error

2014/06/30 15:27:17 PANIC: reflect.Set: **value of type []uint8 is not assignable to type []string**
/usr/lib/go/src/pkg/reflect/value.go:2198 (0x56c152)
    Value.assignTo: panic(context + ": value of type " + v.typ.String() + " is not assignable to type " + dst.String())
/usr/lib/go/src/pkg/reflect/value.go:1385 (0x56966b)
    Value.Set: x = x.assignTo("reflect.Set", v.typ, target)
/usr/lib/go/src/pkg/database/sql/convert.go:215 (0x492d70)
    convertAssign: dv.Set(sv)
/usr/lib/go/src/pkg/database/sql/sql.go:1562 (0x49c0e5)
    (*Rows).Scan: err := convertAssign(dest[i], sv)
/usr/lib/go/src/pkg/database/sql/sql.go:1630 (0x49c560)
    (*Row).Scan: err := r.rows.Scan(dest...)
/home/ceresti/source/gocode/src/ceresti.kilnhg.com/ceresti/server/app/databaseapi.go:144 (0x402478)
    (*coreStruct).GetUserProfile: err := row.Scan(&user.Languages)
/home/ceresti/source/gocode/src/ceresti.kilnhg.com/ceresti/server/app/restfulapi.go:327 (0x40a63c)
    Getuserprofile: userprofileStruct, err := core.GetUserProfile(userId)
/usr/lib/go/src/pkg/runtime/asm_amd64.s:340 (0x4309c2)

DO let me know how to resolve this issue.

  • 写回答

2条回答 默认 最新

  • dongyi1982 2014-07-01 14:10
    关注

    Not all sql databases specify array types (e.g., sqlite3). Go doesn't support any sql implementations directly, but it supplies an implementation-agnostic interface (in the generic sense of the word) for which third-party drivers may be written. Go doesn't impose any limitations on which types its drivers may support, so if a type doesn't cooperate, it's probably the fault of the driver.

    TL;DR: Try getting it as a string

    // if `string` doesn't work, try `[]uint8` or `[]byte` and then convert
    // that output to a string if necessary
    var languages string
    if err := row.Scan(&languages); err != nil {
        // handle error
    }
    
    // depending on how the string is encoded, this may or may not work
    // Update: since you say your list is encoded in the form:
    // `{elem1, elem2, elem3}`, we'll simply ignore the first and last
    // characters and split on ", "
    user.Languages = strings.Split(languages[1:len(languages)-1], ", ")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题