Documentation
¶
Overview ¶
Package uuid provides an implementation of *Universally Unique Identifiers* that can intelligently reorder their bytes, such that V1 (timestamp-based) UUIDs are stored in a "dense" byte order.
This "dense" format has the property that UUIDs which were generated in increasing chronological order will also sort in increasing lexical order. This property makes them suitable for use in an indexed column of a SQL database, as modern SQL database implementations are much happier if their keys are inserted in lexically increasing order.
For more on the byte-reordering technique: https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/
Additionally, instead of using the binary format (hard to read) OR the traditional 8-4-4-4-12 text format (too verbose), we offer a new SQL-friendly text format of "@<base64>". This is about halfway in length between the two standard representations, but is human-readable and can be copy-pasted in a terminal.
A concrete example: the UUID `f8a2571a-8add-11e8-96a8-185e0fad6335` is reordered to "dense" format as `[11 e8 8a dd f8 a2 57 1a 96 a8 ...]`, and this "dense" binary representation is serialized to "@<base64>" format as `@EeiK3fiiVxqWqBheD61jNQ`. (Note that all bytes from `96 a8` onward have the same order in both binary representations.)
Index ¶
- Constants
- func Equal(u1, u2 UUID) bool
- type BinaryMode
- type ParseError
- type Preferences
- func (pref Preferences) FromBytes(in []byte) (UUID, error)
- func (pref Preferences) FromString(in string) (UUID, error)
- func (pref Preferences) MustFromBytes(in []byte) UUID
- func (pref Preferences) MustFromString(in string) UUID
- func (pref Preferences) New() UUID
- func (pref Preferences) Nil() UUID
- func (pref Preferences) String() string
- type TextMode
- type TypeError
- type UUID
- func (uuid UUID) BracketedString() string
- func (uuid UUID) Bytes() []byte
- func (uuid UUID) CanonicalString() string
- func (uuid UUID) DenseBytes() []byte
- func (uuid UUID) DenseString() string
- func (uuid UUID) Equal(other UUID) bool
- func (uuid UUID) Format(s fmt.State, verb rune)
- func (uuid *UUID) FromBytes(in []byte) error
- func (uuid *UUID) FromDenseBytes(in []byte) error
- func (uuid *UUID) FromStandardBytes(in []byte) error
- func (uuid *UUID) FromString(in string) error
- func (uuid UUID) GoString() string
- func (uuid UUID) HashLikeString() string
- func (uuid UUID) IsNil() bool
- func (uuid UUID) IsV1() bool
- func (uuid UUID) IsValid() bool
- func (uuid UUID) MarshalBinary() ([]byte, error)
- func (uuid UUID) MarshalJSON() ([]byte, error)
- func (uuid UUID) MarshalText() ([]byte, error)
- func (uuid *UUID) MustFromBytes(in []byte)
- func (uuid *UUID) MustFromString(in string)
- func (uuid UUID) Preferences() Preferences
- func (uuid *UUID) Scan(value interface{}) error
- func (uuid *UUID) SetNew()
- func (uuid *UUID) SetNil()
- func (uuid *UUID) SetPreferences(pref Preferences)
- func (uuid UUID) StandardBytes() []byte
- func (uuid UUID) String() string
- func (uuid UUID) URNString() string
- func (uuid *UUID) UnmarshalBinary(in []byte) error
- func (uuid *UUID) UnmarshalJSON(in []byte) error
- func (uuid *UUID) UnmarshalText(in []byte) error
- func (uuid UUID) Value() (driver.Value, error)
- func (uuid UUID) Variant() Variant
- func (uuid UUID) Version() Version
- func (uuid UUID) VersionAndVariant() (version Version, variant Variant)
- type ValueMode
- type Variant
- type Version
Constants ¶
const ByteLength = 16
ByteLength is the number of bytes in the binary representation of a UUID.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BinaryMode ¶
type BinaryMode byte
BinaryMode selects the input/output format for parsing/producing binary represenatations.
const ( // StandardOnly: produce RFC 4122 bytes, parse RFC 4122 bytes. StandardOnly BinaryMode // StandardFirst: produce RFC 4122 bytes, parse either byte ordering. // Ambiguous inputs will be interpreted as RFC 4122. StandardFirst // DenseOnly: produce "dense" bytes, parse "dense" bytes. DenseOnly // DenseFirst: produce "dense" bytes, parse either byte ordering. // Ambiguous inputs will be interpreted as "dense". DenseFirst )
BinaryMode enum constants.
func (BinaryMode) String ¶
func (bm BinaryMode) String() string
type ParseError ¶
type ParseError struct { TypeName string MethodName string Input string Detail string ExactInput []byte }
ParseError represents an error in the contents of the input while parsing.
func (ParseError) Error ¶
func (err ParseError) Error() string
type Preferences ¶
type Preferences struct { Value ValueMode Binary BinaryMode Text TextMode }
Preferences represents a combination of modes to apply to a UUID.
func (Preferences) FromBytes ¶
func (pref Preferences) FromBytes(in []byte) (UUID, error)
FromBytes attempts to parse a binary UUID representation using these preferences.
func (Preferences) FromString ¶
func (pref Preferences) FromString(in string) (UUID, error)
FromString attempts to parse a textual UUID representation with these preferences.
func (Preferences) MustFromBytes ¶
func (pref Preferences) MustFromBytes(in []byte) UUID
MustFromBytes parses a binary UUID representation using these preferences, or panics if it cannot.
func (Preferences) MustFromString ¶
func (pref Preferences) MustFromString(in string) UUID
MustFromString parses a textual UUID representation with these preferences, or panics if it cannot.
func (Preferences) New ¶
func (pref Preferences) New() UUID
New returns a newly generated V1 UUID with these preferences.
func (Preferences) Nil ¶
func (pref Preferences) Nil() UUID
Nil returns a Nil-valued UUID with these preferences.
func (Preferences) String ¶
func (pref Preferences) String() string
type TextMode ¶
type TextMode byte
TextMode selects the output format for producing textual representations.
const ( // Dense: "@<base64>", e.g. "@EeiKtHe5nOqWqBheD61jNQ" Dense TextMode // Canonical: "8-4-4-4-12", e.g. "77b99cea-8ab4-11e8-96a8-185e0fad6335" Canonical // HashLike: raw hex digits, e.g. "77b99cea8ab411e896a8185e0fad6335" HashLike // Bracketed: "{8-4-4-4-12}", e.g. "{77b99cea-8ab4-11e8-96a8-185e0fad6335}" Bracketed // URN: URN format, e.g. "urn:uuid:77b99cea-8ab4-11e8-96a8-185e0fad6335" URN )
TextMode enum constants.
type TypeError ¶
type TypeError struct { TypeName string MethodName string Input interface{} InputType reflect.Type ExpectedTypes []reflect.Type }
TypeError represents an error in the Go type of the input while scanning.
type UUID ¶
type UUID struct {
// contains filtered or unexported fields
}
UUID holds a single Universally Unique Identifier.
func FromString ¶
FromString attempts to parse a textual UUID representation.
func MustFromBytes ¶
MustFromBytes parses a binary UUID representation, or panics if it cannot.
func MustFromString ¶
MustFromString parses a textual UUID representation, or panics if it cannot.
func (UUID) BracketedString ¶
BracketedString returns the textual representation of this UUID in "{8-4-4-4-12}" format.
func (UUID) CanonicalString ¶
CanonicalString returns the textual representation of this UUID in canonical "8-4-4-4-12" format.
func (UUID) DenseBytes ¶
DenseBytes returns the binary representation of this UUID in "dense" byte order.
func (UUID) DenseString ¶
DenseString returns the textual representation of this UUID in "@<base64>" format.
func (*UUID) FromDenseBytes ¶
FromDenseBytes attempts to parse a binary UUID representation in "dense" byte order.
func (*UUID) FromStandardBytes ¶
FromStandardBytes attempts to parse a binary UUID representation in RFC 4122 byte order.
func (*UUID) FromString ¶
FromString attempts to parse a textual UUID representation.
func (UUID) GoString ¶
GoString fulfills the "fmt".GoStringer interface. It produces a textual representation of this UUID as a snippet of Go pseudocode.
func (UUID) HashLikeString ¶
HashLikeString returns the textual representation of this UUID as a 32-character hex string.
func (UUID) MarshalBinary ¶
MarshalBinary fulfills the "encoding".BinaryMarshaler interface. It produces a binary representation of this UUID.
func (UUID) MarshalJSON ¶
MarshalJSON fulfills the "encoding/json".Marshaler interface. It produces a textual representation of this UUID as a JSON string.
func (UUID) MarshalText ¶
MarshalText fulfills the "encoding".TextMarshaler interface. It produces a textual representation of this UUID.
func (*UUID) MustFromBytes ¶
MustFromBytes parses a binary UUID representation, or panics if it cannot.
func (*UUID) MustFromString ¶
MustFromString parses a textual UUID representation, or panics if it cannot.
func (UUID) Preferences ¶
func (uuid UUID) Preferences() Preferences
Preferences returns the preference knobs for this object.
func (*UUID) Scan ¶
Scan fulfills the "database/sql".Scanner interface. It attempts to interpret a SQL value as a UUID representation of some kind.
func (*UUID) SetNew ¶
func (uuid *UUID) SetNew()
SetNew updates this UUID to hold a newly generated V1 UUID.
func (*UUID) SetPreferences ¶
func (uuid *UUID) SetPreferences(pref Preferences)
SetPreferences updates the preference knobs for this object.
func (UUID) StandardBytes ¶
StandardBytes returns the binary representation of this UUID in RFC 4122 byte order.
func (UUID) String ¶
String fulfills the "fmt".Stringer interface. It produces a textual representation of this UUID.
func (UUID) URNString ¶
URNString returns the textual representation of this UUID in "urn:uuid:8-4-4-4-12" format.
func (*UUID) UnmarshalBinary ¶
UnmarshalBinary fulfills the "encoding".BinaryUnmarshaler interface. It attempts to parse a binary UUID representation.
func (*UUID) UnmarshalJSON ¶
UnmarshalJSON fulfills the "encoding/json".Unmarshaler interface. It attempts to parse a JSON value as a textual UUID representation.
func (*UUID) UnmarshalText ¶
UnmarshalText fulfills the "encoding".TextUnmarshaler interface. It attempts to parse a textual UUID representation.
func (UUID) Value ¶
Value fulfills the "database/sql/driver".Valuer interface. It produces a SQL representation of the UUID.
func (UUID) VersionAndVariant ¶
VersionAndVariant returns this UUID's Version and Variant. This method is slightly more efficient than calling Version() and Variant() separately.
type ValueMode ¶
type ValueMode byte
ValueMode selects the output behavior of the SQL-oriented Value method.
const ( // Text: output is a string in the current TextMode. Text ValueMode // Binary: output is a byte slice in the current BinaryMode. Binary )
ValueMode enum constants.
type Variant ¶
type Variant byte
Variant indicates the RFC 4122-defined UUID variant.
const ( VariantNCS Variant = 1 VariantRFC4122 Variant = 2 VariantMicrosoft Variant = 3 VariantFuture Variant = 4 )
Variant enum constants.
func (*Variant) FromString ¶
FromString attempts to parse the string representation of a Variant.
type Version ¶
type Version byte
Version indicates the RFC 4122-defined UUID version.
Version enum constants.
func (*Version) FromString ¶
FromString attempts to parse the string representation of a Version.