Scan implements the sql.Scanner interface. A 16-byte slice will be handled by UnmarshalBinary, while a longer byte slice or a string will be handled by UnmarshalText.
(src interface{})
| 46 | // A 16-byte slice will be handled by UnmarshalBinary, while |
| 47 | // a longer byte slice or a string will be handled by UnmarshalText. |
| 48 | func (u *UUID) Scan(src interface{}) error { |
| 49 | switch src := src.(type) { |
| 50 | case UUID: // support gorm convert from UUID to NullUUID |
| 51 | *u = src |
| 52 | return nil |
| 53 | |
| 54 | case []byte: |
| 55 | if len(src) == Size { |
| 56 | return u.UnmarshalBinary(src) |
| 57 | } |
| 58 | return u.UnmarshalText(src) |
| 59 | |
| 60 | case string: |
| 61 | return u.UnmarshalText([]byte(src)) |
| 62 | } |
| 63 | |
| 64 | return fmt.Errorf("uuid: cannot convert %T to UUID", src) |
| 65 | } |
| 66 | |
| 67 | // NullUUID can be used with the standard sql package to represent a |
| 68 | // UUID value that can be NULL in the database. |