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 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在