1 回答
TA贡献1877条经验 获得超1个赞
因此,当您希望 JSON 值来自数据库并自动(取消)编组时,您需要为此创建一个类型:
type Programs struct {
ID int `json:"id"`
ShortName string `json:"short_name"`
ProgramPoints float64 `json:"program_points"`
Countries Countries `json:"countries"`
}
type Countries []string
func (c Countries) Value() (driver.Value, error) {
return json.Marshal(c) // return json marshalled value
}
func (c *Countries) Scan(v interface{}) error {
switch tv := v.(type) {
case []byte:
return json.Unmarshal(tv, &c) // unmarshal
case []uint8:
return json.Unmarshal([]byte(tv), &c) // can't remember the specifics, but this may be needed
}
return errors.New("unsupported type")
}
那应该处理这些stmt.Scan东西
- 1 回答
- 0 关注
- 261 浏览
添加回答
举报
