gorm不支持的数据类型接口
gorm不支持的数据类型接口 如何将不同的结构保存到json(gorm)中的一个字段?需要类型检查,而不仅仅是Json中的序列化 错误代码示例:不支持的数据类型:models.Fieldsgorm不支持的数据类型接口,go,go-gorm,Go,Go Gorm,gorm不支持的数据类型接口 如何将不同的结构保存到json(gorm)中的一个字段?需要类型检查,而不仅仅是Json中的序列化 错误代码示例:不支持的数据类型:models.Fields // table questions (GORM) type Questions struct { ID Fields `json:"fields"` // ..... } type Fields interface{ // ... } // radio ----
// table questions (GORM)
type Questions struct {
ID
Fields `json:"fields"`
// .....
}
type Fields interface{
// ...
}
// radio ----------
type Radio struct {
Text string
Img string
// ...
}
func (d Radio) Value() (driver.Value, error) {
return json.Marshal(d)
}
func (d *Radio) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}
return json.Unmarshal(b, &d)
}
// Checkbox ----------
type Checkbox struct {
Text string
MaxCheckbox uint16
// ...
}
func (d Checkbox) Value() (driver.Value, error) {
return json.Marshal(d)
}
func (d *Checkbox) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}
return json.Unmarshal(b, &d)
}
您可能已经解决了这个问题,但是您需要使用。您可能已经解决了这个问题,但是您需要使用