package dts import ( "database/sql" "database/sql/driver" "encoding/json" ) type NullUint struct { V uint Valid bool } func (nu *NullUint) Scan(value any) error { sn := new(sql.Null[uint]) err := sn.Scan(value) nu.V = sn.V nu.Valid = sn.Valid return err } func (nu NullUint) Value() (driver.Value, error) { if !nu.Valid { return nil, nil } // 注意:driver.Value 不支持 uint return int64(nu.V), nil } func (nu NullUint) MarshalJSON() ([]byte, error) { if nu.Valid { return json.Marshal(nu.V) } return json.Marshal(nil) } func (nu *NullUint) UnmarshalJSON(b []byte) error { if string(b) == "null" { nu.Valid = false return nil } err := json.Unmarshal(b, &nu.V) if err == nil { nu.Valid = true } return err }