1 回答
TA貢獻1829條經驗 獲得超4個贊
你可以讓你的metas結構實現sql.Scanner接口
它應該是這樣的:
func (m *metas) Scan(src interface{}) error {
strValue, ok := src.(string)
if !ok {
return fmt.Errorf("metas field must be a string, got %T instead", src)
}
return json.Unmarshal([]byte(strValue), m)
}
之后,您可以將其用作InfoClip字段并將其直接傳遞給Scan并刪除decodeJsonSql:
type InfoClip struct {
// [...]
Metas metas `json:metas`
// [...]
}
和
q := "SELECT c.id AS clipId, c.streamUrl, c.startTimecode, c.endTimecode, c.createdAt, s.metas,... FROM clips WHERE c.id = ?"
row := datab.QueryRow(q, mediaId)
clips := InfoClip{}
err := row.Scan(&clips.ClipId, &clips.StreamUrl, &clips.StartTimeCode, &clips.EndTimeCode, &clips.CreatedAt, &clips.Metas) // [...]
if err != nil {
fmt.Printf("SQL Err: %s", err)
return err
}
(順便說一句,你可以看到,我換成datab.Query與datab.QueryRow你期待只有一個結果)
- 1 回答
- 0 關注
- 245 瀏覽
添加回答
舉報
