我是新手,正在尝试从 MySQL 读取数据。架构:“id:INT(11),头部:TEXT,过滤器:BIT(64)”我试图以常见的方式做到这一点:type News struct { Id int `field:"id"` Head string `field:"head"` Filter uint64 `field:"filter"`}...rows.Scan(&item.Id, &item.Head, &item.Filter)并得到:Scan error on column index 2, name "filter": converting NULL to uint64 is unsupported我尝试了一些带有反射的示例,但没有结果。我试过像这里一样制作自己的类型(真的不明白这个):type BitMask uint64func (bits *BitMask) Scan(src interface{}) error { str, ok := src.(string) if !ok { return fmt.Errorf("Unexpected type for BitMask: %T\n", src) } fmt.Println(str) //var v uint64 = 0 //*bits = v // <- Also have error here return nil}并得到类似的错误:“BitMask 的意外类型:< nil>”
1 回答

哈士奇WWW
TA贡献1799条经验 获得超6个赞
我做出这个答案只是为了结束问题并总结结果。所以有2种方式:
1 编写自己的类型:
type BitMask sql.NullInt64
func (bits *BitMask) Scan(src interface{}) error {
if src == nil {
bits.Int64 = 0
bits.Valid = false
return nil
}
buf, ok := src.([]byte)
if !ok {
return fmt.Errorf("Unexpected type for BitMask: %T\n", src)
}
if len(buf) != 8 {
bits.Int64 = 0
bits.Valid = false
return errors.New("Size of bit filed is not 8 bytes\n")
}
bits.Int64 = 0
for i := range buf {
bits.Int64 *= 256
bits.Int64 = int64(buf[i])
}
bits.Valid = true
return nil
}
不要使用BIT(64),UNSIGNED BIGINT而是。因为没有优势,只有问题。(这就是我要做的)
- 1 回答
- 0 关注
- 172 浏览
添加回答
举报
0/150
提交
取消