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 關注
- 215 瀏覽
添加回答
舉報