golang -- cannot convert raw (variable of type []byte) to driver.Value

Viewed 39

I'm trying to create a Value function for a jsonb database field, for use by go-pg.

func (m JSONMap) Value() (driver.Value, error) {
    raw, err := json.Marshal(m)

    if err != nil {
        return driver.Value(nil), err
    }

    return driver.Value(raw), nil
}

This seems to be a pretty standard piece of code. Many more or less identical pieces of code can be found here: https://golang.hotexamples.com/examples/database.sql.driver/-/Value/golang-value-function-examples.html and very similar code can be found in the go-pg tests: https://github.com/go-pg/pg/blob/782c9d35ba243106ba6445fc753c3ac6a14c3324/conv_test.go

But no matter what I do, I get a compiler error: cannot convert raw (variable of type []byte) to driver.Value

If I replace return driver.Value(raw), nil with return driver.Value("ABC"), nil it seems to work just fine. However,

str := "ABC"
return driver.Value(str), nil

generates a compiler error.

I'm a little lost, any help is appreciated.

0 Answers
Related