我不确定xorm可以做什么/如果可以,但您可以创建一个类型并为其实现Valuer和Scanner接口。这是一个示例,我为使用 abit(1)进行了拉取请求bool。
https://github.com/jmoiron/sqlx/blob/master/types/types.go#L152
对于整数,您只需返回包含的int而不是 a 。像这样:[]byteint
type IntBool bool
// Value implements the driver.Valuer interface,
// and turns the IntBool into an integer for MySQL storage.
func (i IntBool) Value() (driver.Value, error) {
if i {
return 1, nil
}
return 0, nil
}
// Scan implements the sql.Scanner interface,
// and turns the int incoming from MySQL into an IntBool
func (i *IntBool) Scan(src interface{}) error {
v, ok := src.(int)
if !ok {
return errors.New("bad int type assertion")
}
*i = v == 1
return nil
}
然后您的结构将只使用新类型
type myStruct struct {
isDeleted IntBool `xorm:"'isDeleted' tinyint(3)"`
}
但是,再一次,您将这个布尔值声明为 a 有什么特别的原因tinyint吗?MySQL有一个布尔类型,一切都可以正常工作。