// Copyright 2021 Google Inc. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package uuidimport ()var jsonNull = []byte("null")// NullUUID represents a UUID that may be null.// NullUUID implements the SQL driver.Scanner interface so// it can be used as a scan destination://// var u uuid.NullUUID// err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&u)// ...// if u.Valid {// // use u.UUID// } else {// // NULL value// }//typeNullUUIDstruct { UUID UUID Valid bool// Valid is true if UUID is not NULL}// Scan implements the SQL driver.Scanner interface.func ( *NullUUID) ( interface{}) error {if == nil { .UUID, .Valid = Nil, falsereturnnil } := .UUID.Scan()if != nil { .Valid = falsereturn } .Valid = truereturnnil}// Value implements the driver Valuer interface.func ( NullUUID) () (driver.Value, error) {if !.Valid {returnnil, nil }// Delegate to UUID Value functionreturn .UUID.Value()}// MarshalBinary implements encoding.BinaryMarshaler.func ( NullUUID) () ([]byte, error) {if .Valid {return .UUID[:], nil }return []byte(nil), nil}// UnmarshalBinary implements encoding.BinaryUnmarshaler.func ( *NullUUID) ( []byte) error {iflen() != 16 {returnfmt.Errorf("invalid UUID (got %d bytes)", len()) }copy(.UUID[:], ) .Valid = truereturnnil}// MarshalText implements encoding.TextMarshaler.func ( NullUUID) () ([]byte, error) {if .Valid {return .UUID.MarshalText() }returnjsonNull, nil}// UnmarshalText implements encoding.TextUnmarshaler.func ( *NullUUID) ( []byte) error { , := ParseBytes()if != nil { .Valid = falsereturn } .UUID = .Valid = truereturnnil}// MarshalJSON implements json.Marshaler.func ( NullUUID) () ([]byte, error) {if .Valid {returnjson.Marshal(.UUID) }returnjsonNull, nil}// UnmarshalJSON implements json.Unmarshaler.func ( *NullUUID) ( []byte) error {ifbytes.Equal(, jsonNull) { * = NullUUID{}returnnil// valid null UUID } := json.Unmarshal(, &.UUID) .Valid = == nilreturn}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.