duanlijia5864 2018-02-12 07:18
浏览 38
已采纳

使用扫描将db值强制转换为特定类型

I'm trying to read a UUID retrieved from Postgres, using github.com/jackc/pgx, into a variable of type uuid.UUID (From the github.com/google/uuid package).

An example code could be:

var dbId = uuid.UUID 
err = db.Pool.QueryRow("SELECT id FROM users WHERE objectname = $1;", objectUUID.String()).Scan(&dbId)
  if err != nil {
    log.Printf("Failed to fetch from database: %v", err)
    return
  }

The quickfix is to store the dbId in a temporary variable and then later convert said temporary variable into the correct type, but I have a feeling that there is a better, or more idiomatic, way to do it.

The error I'm getting is:

2018/02/12 07:09:18 handlers.go:187: Failed to fetch from database: can't scan into dest[1]: cannot assign &{[127 122 68 237 130 120 65 78 159 189 9 188 27 48 117 88] 2} into *uuid.UUID
  • 写回答

1条回答 默认 最新

  • doucaishi0077 2018-02-12 07:37
    关注

    The errors says that you are trying to Scan a struct pointer to the UUID pointer. The struct is defined here. So basically you should use this struct and Scan it into that and then convert the byte array in the struct to whatever UUID type you are using.

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

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功