#TIL 20 - How to fix unsupported Scan, storing driver.Value type []uint8 into type

Oct 20, 2021 · Dung Huynh

What

Fix "unsupported Scan" error when scanning JSON database columns into custom Go types.

Why

sqlx can't automatically scan JSON/JSONB columns into custom structs. Need custom Scan method.

How

Implement sql.Scanner interface:

type Registries []Registry

func (r *Registries) Scan(src interface{}) error {
    var data []byte
    switch v := src.(type) {
    case []byte:
        data = v
    case string:
        data = []byte(v)
    case nil:
        return nil
    }
    return json.Unmarshal(data, r)
}