Documentation ¶
Overview ¶
Package uuid contains functions for creating and working with unique IDs in RFC 4122.
The main difference from other similar packages:
1. support only versions of UUID V4
2. full support for serialization/deserialization to text and binary form, including JSON, BSON, XML and databases.
Example ¶
package main import ( "encoding/json" "log" "github.com/globalsign/mgo/bson" "github.com/mdigger/uuid" ) func main() { uuidData := uuid.New() println("UUID: ", uuidData.String()) data, err := json.Marshal(uuidData) if err != nil { log.Fatal(err) } println("JSON: ", string(data)) var newUUID uuid.UUID if err := json.Unmarshal(data, &newUUID); err != nil { log.Fatal(err) } println("RESTORE:", newUUID.String()) data, err = bson.Marshal(uuidData) if err != nil { log.Fatal(err) } if err := bson.Unmarshal(data, &newUUID); err != nil { log.Fatal(err) } println("RESTORE:", newUUID.String()) }
Output:
Index ¶
- type UUID
- func (u UUID) Bytes() []byte
- func (u UUID) Equal(uuid UUID) bool
- func (u UUID) GetBSON() (interface{}, error)
- func (u UUID) MarshalBinary() (data []byte, err error)
- func (u UUID) MarshalText() ([]byte, error)
- func (u *UUID) Scan(src interface{}) error
- func (u *UUID) SetBSON(raw bson.Raw) error
- func (u UUID) String() string
- func (u *UUID) UnmarshalBinary(data []byte) error
- func (u *UUID) UnmarshalText(text []byte) (err error)
- func (u UUID) Value() (driver.Value, error)
- func (u UUID) Version() uint
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type UUID ¶
type UUID [16]byte
UUID describes the format of the unique identifier corresponding to RFC 4122.
func (UUID) GetBSON ¶
GetBSON returns a representation of the unique identifier in the form of the BSON binary object with the set type UUID.
func (UUID) MarshalBinary ¶
MarshalBinary provides the HMDI supports the interface encoding.BinaryMarshaler.
func (UUID) MarshalText ¶
MarshalText provides the HMDI supports the interface encoding.TextMarshaler. The result of the encoding corresponds exactly to the canonical string representation.
func (*UUID) Scan ¶
Scan provides support for the sql interface.Scanner. For the 16 byte sequence is used UnmarshalBinary, whereas the longer sequence, or string is used UnmarshalText.
func (*UUID) SetBSON ¶
SetBSON deserializes the UUID from the internal binary representation of JSON.
func (UUID) String ¶
String returns the canonical string representation of a UUID:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
func (*UUID) UnmarshalBinary ¶
UnmarshalBinary provides support for the interface encoding.BinaryUnmarshaler. Returns an error if data size is not equal to 16 bytes.
func (*UUID) UnmarshalText ¶
UnmarshalText provides support for the interface encoding.TextUnmarshaler. The following formats are supported:
"6ba7b810-9dad-11d1-80b4-00c04fd430c8", "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"