duancuisan2503 2015-03-21 16:14
浏览 414
已采纳

golang反射,获取指向结构字段值的指针

I'm trying to make a function that converts a struct in the way mysql rows.Scan function needs it, so I don't need to pass manually lots of parameters.

Note: I know the existence of sqlx and the alternative of writing manually in separate lines every pointer, but I'd like to solve it in this way as I'm learning go and want to understand what's going on.

The error I get with this solution is:
panic: sql: Scan error on column index 0: destination not a pointer to me looks like valueField.Addr().Pointer() should be a Pointer to the value. The following is a simplification of my code.

type User struct {
    Name string
    Age  int
}

func StrutForScan(u interface{}) []interface{} {
    val := reflect.ValueOf(u).Elem()
    v := make([]interface{}, val.NumField())
    for i := 0; i < val.NumField(); i++ {
        valueField := val.Field(i)
        v[i] = valueField.Addr().Pointer()
    }
    return v
}

func ListUsers {
    rows, err := db.Query("SELECT * FROM users")
    PanicIf(err)
    var user User
    for rows.Next() {
        err := rows.Scan(StrutForScan(&user)...)
        PanicIf(err)
        fmt.Printf("
Name: %s, Age: %s", user.Name, string(user.Age))

    }
}
  • 写回答

1条回答 默认 最新

  • douyiji3919 2015-03-21 16:55
    关注

    You need to use .Interface() not .Pointer()

    func StrutForScan(u interface{}) []interface{} {
        val := reflect.ValueOf(u).Elem()
        v := make([]interface{}, val.NumField())
        for i := 0; i < val.NumField(); i++ {
            valueField := val.Field(i)
            v[i] = valueField.Addr().Interface()
        }
        return v
    }
    

    The reason behind that is that .Pointer() returns an actual "pointer" to the data, you can't do much with it without using the unsafe package.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?
  • ¥20 求会6sv辐射传输模型,辅导(可py6s🙏🏻有偿