Skip to content

Instantly share code, notes, and snippets.

@nanmu42
Created October 23, 2019 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanmu42/36526e24b86f487826b059deccb81229 to your computer and use it in GitHub Desktop.
Save nanmu42/36526e24b86f487826b059deccb81229 to your computer and use it in GitHub Desktop.
Golang SQL scanner and valuer example
// QueryParams every key is a query param
type QueryParams map[string]QueryParam
// Scan satisfies SQL scanner interface
func (q *QueryParams) Scan(src interface{}) error {
b, ok := src.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}
return json.Unmarshal(b, q)
}
// Value satisfies SQL Valuer interface
func (q QueryParams) Value() (driver.Value, error) {
return json.Marshal(q)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment