import "github.com/gofrs/uuid"
Package uuid provides implementations of the Universally Unique Identifier (UUID), as specified in RFC-4122,
RFC-4122[1] provides the specification for versions 1, 3, 4, and 5.
DCE 1.1[2] provides the specification for version 2, but version 2 support was removed from this package in v4 due to some concerns with the specification itself. Reading the spec, it seems that it would result in generating UUIDs that aren't very unique. In having read the spec it seemed that our implementation did not meet the spec. It also seems to be at-odds with RFC 4122, meaning we would need quite a bit of special code to support it. Lastly, there were no Version 2 implementations that we could find to ensure we were understanding the specification correctly.
[1] https://tools.ietf.org/html/rfc4122 [2] http://pubs.opengroup.org/onlinepubs/9696989899/chap5.htm#tagcjh_08_02_01_01
codec.go generator.go sql.go uuid.go
const ( V1 byte // Version 1 (date-time and MAC address) V3 // Version 3 (namespace name-based) V4 // Version 4 (random) V5 // Version 5 (namespace name-based) )
UUID versions.
UUID layout variants.
UUID DCE domains.
const Size = 16
Size of a UUID in bytes.
var ( NamespaceDNS = Must(FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")) NamespaceURL = Must(FromString("6ba7b811-9dad-11d1-80b4-00c04fd430c8")) NamespaceOID = Must(FromString("6ba7b812-9dad-11d1-80b4-00c04fd430c8")) NamespaceX500 = Must(FromString("6ba7b814-9dad-11d1-80b4-00c04fd430c8")) )
Predefined namespace UUIDs.
Nil is the nil UUID, as specified in RFC-4122, that has all 128 bits set to zero.
type Gen struct {
// contains filtered or unexported fields
}
Gen is a reference UUID generator based on the specifications laid out in RFC-4122 and DCE 1.1: Authentication and Security Services. This type satisfies the Generator interface as defined in this package.
For consumers who are generating V1 UUIDs, but don't want to expose the MAC address of the node generating the UUIDs, the NewGenWithHWAF() function has been provided as a convenience. See the function's documentation for more info.
The authors of this package do not feel that the majority of users will need to obfuscate their MAC address, and so we recommend using NewGen() to create a new generator.
NewGen returns a new instance of Gen with some default values set. Most people should use this.
func NewGenWithHWAF(hwaf HWAddrFunc) *Gen
NewGenWithHWAF builds a new UUID generator with the HWAddrFunc provided. Most consumers should use NewGen() instead.
This is used so that consumers can generate their own MAC addresses, for use in the generated UUIDs, if there is some concern about exposing the physical address of the machine generating the UUID.
The Gen generator will only invoke the HWAddrFunc once, and cache that MAC address for all the future UUIDs generated by it. If you'd like to switch the MAC address being used, you'll need to create a new generator using this function.
NewV1 returns a UUID based on the current timestamp and MAC address.
NewV3 returns a UUID based on the MD5 hash of the namespace UUID and name.
NewV4 returns a randomly generated UUID.
NewV5 returns a UUID based on SHA-1 hash of the namespace UUID and name.
type Generator interface { NewV1() (UUID, error) NewV3(ns UUID, name string) UUID NewV4() (UUID, error) NewV5(ns UUID, name string) UUID }
Generator provides an interface for generating UUIDs.
DefaultGenerator is the default UUID Generator used by this package.
type HWAddrFunc func() (net.HardwareAddr, error)
HWAddrFunc is the function type used to provide hardware (MAC) addresses.
NullUUID can be used with the standard sql package to represent a UUID value that can be NULL in the database.
MarshalJSON marshals the NullUUID as null or the nested UUID
Scan implements the sql.Scanner interface.
UnmarshalJSON unmarshals a NullUUID
Value implements the driver.Valuer interface.
Timestamp is the count of 100-nanosecond intervals since 00:00:00.00, 15 October 1582 within a V1 UUID. This type has no meaning for other UUID versions since they don't have an embedded timestamp.
TimestampFromV1 returns the Timestamp embedded within a V1 UUID. Returns an error if the UUID is any version other than 1.
Time returns the UTC time.Time representation of a Timestamp
UUID is an array type to represent the value of a UUID, as defined in RFC-4122.
FromBytes returns a UUID generated from the raw byte slice input. It will return an error if the slice isn't 16 bytes long.
FromBytesOrNil returns a UUID generated from the raw byte slice input. Same behavior as FromBytes(), but returns uuid.Nil instead of an error.
FromString returns a UUID parsed from the input string. Input is expected in a form accepted by UnmarshalText.
FromStringOrNil returns a UUID parsed from the input string. Same behavior as FromString(), but returns uuid.Nil instead of an error.
Must is a helper that wraps a call to a function returning (UUID, error) and panics if the error is non-nil. It is intended for use in variable initializations such as
var packageUUID = uuid.Must(uuid.FromString("123e4567-e89b-12d3-a456-426655440000"))
NewV1 returns a UUID based on the current timestamp and MAC address.
NewV3 returns a UUID based on the MD5 hash of the namespace UUID and name.
NewV4 returns a randomly generated UUID.
NewV5 returns a UUID based on SHA-1 hash of the namespace UUID and name.
Bytes returns a byte slice representation of the UUID.
Format implements fmt.Formatter for UUID values.
The behavior is as follows: The 'x' and 'X' verbs output only the hex digits of the UUID, using a-f for 'x' and A-F for 'X'. The 'v', '+v', 's' and 'q' verbs return the canonical RFC-4122 string representation. The 'S' verb returns the RFC-4122 format, but with capital hex digits. The '#v' verb returns the "Go syntax" representation, which is a 16 byte array initializer. All other verbs not handled directly by the fmt package (like '%p') are unsupported and will return "%!verb(uuid.UUID=value)" as recommended by the fmt package.
MarshalBinary implements the encoding.BinaryMarshaler interface.
MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by the String() method.
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.
SetVariant sets the variant bits.
SetVersion sets the version bits.
String returns a canonical RFC-4122 string representation of the UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It will return an error if the slice isn't 16 bytes long.
UnmarshalText implements the encoding.TextUnmarshaler interface. Following formats are supported:
"6ba7b810-9dad-11d1-80b4-00c04fd430c8", "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" "6ba7b8109dad11d180b400c04fd430c8" "{6ba7b8109dad11d180b400c04fd430c8}", "urn:uuid:6ba7b8109dad11d180b400c04fd430c8"
ABNF for supported UUID text representation follows:
URN := 'urn' UUID-NID := 'uuid' hexdig := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' hexoct := hexdig hexdig 2hexoct := hexoct hexoct 4hexoct := 2hexoct 2hexoct 6hexoct := 4hexoct 2hexoct 12hexoct := 6hexoct 6hexoct hashlike := 12hexoct canonical := 4hexoct '-' 2hexoct '-' 2hexoct '-' 6hexoct plain := canonical | hashlike uuid := canonical | hashlike | braced | urn braced := '{' plain '}' | '{' hashlike '}' urn := URN ':' UUID-NID ':' plain
Value implements the driver.Valuer interface.
Variant returns the UUID layout variant.
Version returns the algorithm version used to generate the UUID.
Package uuid imports 15 packages (graph) and is imported by 1161 packages. Updated 2020-12-30. Refresh now. Tools for package owners.