mysql - 如何在 golang 中获取 MySQL 的 BIT(64)
问题描述
我是新手,正在尝试从 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 uint64
func (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>”
解决方案
我做出这个答案只是为了结束问题并总结结果。所以有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
而是。因为没有优势,只有问题。(这就是我要做的)
推荐阅读
- amazon-web-services - 如何在 AWS CLI 中注册创建的 webhook?
- windows - 在 Windows Kafka 上删除主题时出现 AccessDeniedException
- java - 具有未知实体的 MappingException
- c# - int 和 string 之间的唯一索引
- java - 我的列表视图没有刷新。该怎么办?
- php - 在 Yii2 中使用数据库中的表单字段时如何修复“获取未知属性”错误?
- html - 如何制作响应式图片
- ffmpeg - ffmpeg 混合过滤器如何工作
- java - 如何在工具栏前面锚定视图?
- javascript - 显示/隐藏 DIV 元素 Javascript 的多次不需要的点击