Movatterモバイル変換


[0]ホーム

URL:


uuid

packagemodule
v1.6.0Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License:BSD-3-ClauseImports:17Imported by:102,317

Details

Repository

github.com/google/uuid

Links

README

uuid

The uuid package generates and inspects UUIDs based onRFC 4122and DCE 1.1: Authentication and Security Services.

This package is based on the github.com/pborman/uuid package (previously namedcode.google.com/p/go-uuid). It differs from these earlier packages in thata UUID is a 16 byte array rather than a byte slice. One loss due to thischange is the ability to represent an invalid UUID (vs a NIL UUID).

Install
go get github.com/google/uuid
Documentation

Go Reference

Fullgo doc style documentation for the package can be viewed online withoutinstalling this package by using the GoDoc site here:http://pkg.go.dev/github.com/google/uuid

Documentation

Overview

Package uuid generates and inspects UUIDs.

UUIDs are based onRFC 4122 and DCE 1.1: Authentication and SecurityServices.

A UUID is a 16 byte (128 bit) array. UUIDs may be used as keys tomaps or compared directly.

Index

Constants

View Source
const (Person =Domain(0)Group  =Domain(1)Org    =Domain(2))

Domain constants for DCE Security (Version 2) UUIDs.

View Source
const (Invalid   =Variant(iota)// Invalid UUIDRFC4122// The variant specified in RFC4122Reserved// Reserved, NCS backward compatibility.Microsoft// Reserved, Microsoft Corporation backward compatibility.Future// Reserved for future definition.)

Constants returned by Variant.

Variables

View Source
var (NameSpaceDNS  =Must(Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8"))NameSpaceURL  =Must(Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8"))NameSpaceOID  =Must(Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8"))NameSpaceX500 =Must(Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8"))NilUUID// empty UUID, all zeros// The Max UUID is special form of UUID that is specified to have all 128 bits set to 1.Max =UUID{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,})

Well known namespace IDs and UUIDs

Functions

funcClockSequence

func ClockSequence()int

ClockSequence returns the current clock sequence, generating one if notalready set. The clock sequence is only used for Version 1 UUIDs.

The uuid package does not use global static storage for the clock sequence orthe last time a UUID was generated. Unless SetClockSequence is used, a newrandom clock sequence is generated the first time a clock sequence isrequested by ClockSequence, GetTime, or NewUUID. (section 4.2.1.1)

funcDisableRandPooladded inv1.3.0

func DisableRandPool()

DisableRandPool disables the randomness pool if it was previouslyenabled with EnableRandPool.

Both EnableRandPool and DisableRandPool are not thread-safe and shouldonly be called when there is no possibility that New or any otherUUID Version 4 generation function will be called concurrently.

funcEnableRandPooladded inv1.3.0

func EnableRandPool()

EnableRandPool enables internal randomness pool used for Random(Version 4) UUID generation. The pool contains random bytes read fromthe random number generator on demand in batches. Enabling the poolmay improve the UUID generation throughput significantly.

Since the pool is stored on the Go heap, this feature may be a bad fitfor security sensitive applications.

Both EnableRandPool and DisableRandPool are not thread-safe and shouldonly be called when there is no possibility that New or any otherUUID Version 4 generation function will be called concurrently.

funcIsInvalidLengthErroradded inv1.3.0

func IsInvalidLengthError(errerror)bool

IsInvalidLengthError is matcher function for custom error invalidLengthError

funcNewStringadded inv1.2.0

func NewString()string

NewString creates a new random UUID and returns it as a string or panics.NewString is equivalent to the expression

uuid.New().String()

funcNodeID

func NodeID() []byte

NodeID returns a slice of a copy of the current Node ID, setting the Node IDif not already set.

funcNodeInterface

func NodeInterface()string

NodeInterface returns the name of the interface from which the NodeID wasderived. The interface "user" is returned if the NodeID was set bySetNodeID.

funcSetClockSequence

func SetClockSequence(seqint)

SetClockSequence sets the clock sequence to the lower 14 bits of seq. Setting to-1 causes a new sequence to be generated.

funcSetNodeID

func SetNodeID(id []byte)bool

SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytesof id are used. If id is less than 6 bytes then false is returned and theNode ID is not set.

funcSetNodeInterface

func SetNodeInterface(namestring)bool

SetNodeInterface selects the hardware address to be used for Version 1 UUIDs.If name is "" then the first usable interface found will be used or a randomNode ID will be generated. If a named interface cannot be found then falseis returned.

SetNodeInterface never fails when name is "".

funcSetRand

func SetRand(rio.Reader)

SetRand sets the random number generator to r, which implements io.Reader.If r.Read returns an error when the package requests random data thena panic will be issued.

Calling SetRand with nil sets the random number generator to the defaultgenerator.

funcValidateadded inv1.5.0

func Validate(sstring)error

Validate returns an error if s is not a properly formatted UUID in one of the following formats:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxurn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

It returns an error if the format is invalid, otherwise nil.

Types

typeDomain

type Domainbyte

A Domain represents a Version 2 domain

func (Domain)String

func (dDomain) String()string

typeNullUUIDadded inv1.3.0

type NullUUID struct {UUIDUUIDValidbool// Valid is true if UUID is not NULL}

NullUUID represents a UUID that may be null.NullUUID implements the SQL driver.Scanner interface soit can be used as a scan destination:

var u uuid.NullUUIDerr := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&u)...if u.Valid {   // use u.UUID} else {   // NULL value}

func (NullUUID)MarshalBinaryadded inv1.3.0

func (nuNullUUID) MarshalBinary() ([]byte,error)

MarshalBinary implements encoding.BinaryMarshaler.

func (NullUUID)MarshalJSONadded inv1.3.0

func (nuNullUUID) MarshalJSON() ([]byte,error)

MarshalJSON implements json.Marshaler.

func (NullUUID)MarshalTextadded inv1.3.0

func (nuNullUUID) MarshalText() ([]byte,error)

MarshalText implements encoding.TextMarshaler.

func (*NullUUID)Scanadded inv1.3.0

func (nu *NullUUID) Scan(value interface{})error

Scan implements the SQL driver.Scanner interface.

func (*NullUUID)UnmarshalBinaryadded inv1.3.0

func (nu *NullUUID) UnmarshalBinary(data []byte)error

UnmarshalBinary implements encoding.BinaryUnmarshaler.

func (*NullUUID)UnmarshalJSONadded inv1.3.0

func (nu *NullUUID) UnmarshalJSON(data []byte)error

UnmarshalJSON implements json.Unmarshaler.

func (*NullUUID)UnmarshalTextadded inv1.3.0

func (nu *NullUUID) UnmarshalText(data []byte)error

UnmarshalText implements encoding.TextUnmarshaler.

func (NullUUID)Valueadded inv1.3.0

func (nuNullUUID) Value() (driver.Value,error)

Value implements the driver Valuer interface.

typeTime

type Timeint64

A Time represents a time as the number of 100's of nanoseconds since 15 Oct1582.

funcGetTime

func GetTime() (Time,uint16,error)

GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) andclock sequence as well as adjusting the clock sequence as needed. An erroris returned if the current time cannot be determined.

func (Time)UnixTime

func (tTime) UnixTime() (sec, nsecint64)

UnixTime converts t the number of seconds and nanoseconds using the Unixepoch of 1 Jan 1970.

typeUUID

type UUID [16]byte

A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined inRFC4122.

funcFromBytes

func FromBytes(b []byte) (uuidUUID, errerror)

FromBytes creates a new UUID from a byte slice. Returns an error if the slicedoes not have a length of 16. The bytes are copied from the slice.

funcMust

func Must(uuidUUID, errerror)UUID

Must returns uuid if err is nil and panics otherwise.

funcMustParseadded inv1.1.0

func MustParse(sstring)UUID

MustParse is like Parse but panics if the string cannot be parsed.It simplifies safe initialization of global variables holding compiled UUIDs.

funcNew

func New()UUID

New creates a new random UUID or panics. New is equivalent tothe expression

uuid.Must(uuid.NewRandom())

funcNewDCEGroup

func NewDCEGroup() (UUID,error)

NewDCEGroup returns a DCE Security (Version 2) UUID in the groupdomain with the id returned by os.Getgid.

NewDCESecurity(Group, uint32(os.Getgid()))

funcNewDCEPerson

func NewDCEPerson() (UUID,error)

NewDCEPerson returns a DCE Security (Version 2) UUID in the persondomain with the id returned by os.Getuid.

NewDCESecurity(Person, uint32(os.Getuid()))

funcNewDCESecurity

func NewDCESecurity(domainDomain, iduint32) (UUID,error)

NewDCESecurity returns a DCE Security (Version 2) UUID.

The domain should be one of Person, Group or Org.On a POSIX system the id should be the users UID for the Persondomain and the users GID for the Group. The meaning of id forthe domain Org or on non-POSIX systems is site defined.

For a given domain/id pair the same token may be returned for up to7 minutes and 10 seconds.

funcNewHash

func NewHash(hhash.Hash, spaceUUID, data []byte, versionint)UUID

NewHash returns a new UUID derived from the hash of space concatenated withdata generated by h. The hash should be at least 16 byte in length. Thefirst 16 bytes of the hash are used to form the UUID. The version of theUUID will be the lower 4 bits of version. NewHash is used to implementNewMD5 and NewSHA1.

funcNewMD5

func NewMD5(spaceUUID, data []byte)UUID

NewMD5 returns a new MD5 (Version 3) UUID based on thesupplied name space and data. It is the same as calling:

NewHash(md5.New(), space, data, 3)

funcNewRandom

func NewRandom() (UUID,error)

NewRandom returns a Random (Version 4) UUID.

The strength of the UUIDs is based on the strength of the crypto/randpackage.

Uses the randomness pool if it was enabled with EnableRandPool.

A note about uniqueness derived from the UUID Wikipedia entry:

Randomly generated UUIDs have 122 random bits.  One's annual risk of beinghit by a meteorite is estimated to be one chance in 17 billion, thatmeans the probability is about 0.00000000006 (6 × 10−11),equivalent to the odds of creating a few tens of trillions of UUIDs in ayear and having one duplicate.

funcNewRandomFromReaderadded inv1.1.2

func NewRandomFromReader(rio.Reader) (UUID,error)

NewRandomFromReader returns a UUID based on bytes read from a given io.Reader.

funcNewSHA1

func NewSHA1(spaceUUID, data []byte)UUID

NewSHA1 returns a new SHA1 (Version 5) UUID based on thesupplied name space and data. It is the same as calling:

NewHash(sha1.New(), space, data, 5)

funcNewUUID

func NewUUID() (UUID,error)

NewUUID returns a Version 1 UUID based on the current NodeID and clocksequence, and the current time. If the NodeID has not been set by SetNodeIDor SetNodeInterface then it will be set automatically. If the NodeID cannotbe set NewUUID returns nil. If clock sequence has not been set bySetClockSequence then it will be set automatically. If GetTime fails toreturn the current NewUUID returns nil and an error.

In most cases, New should be used.

funcNewV6added inv1.5.0

func NewV6() (UUID,error)

UUID version 6 is a field-compatible version of UUIDv1, reordered for improved DB locality.It is expected that UUIDv6 will primarily be used in contexts where there are existing v1 UUIDs.Systems that do not involve legacy UUIDv1 SHOULD consider using UUIDv7 instead.

seehttps://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-03#uuidv6

NewV6 returns a Version 6 UUID based on the current NodeID and clocksequence, and the current time. If the NodeID has not been set by SetNodeIDor SetNodeInterface then it will be set automatically. If the NodeID cannotbe set NewV6 set NodeID is random bits automatically . If clock sequence has not been set bySetClockSequence then it will be set automatically. If GetTime fails toreturn the current NewV6 returns Nil and an error.

funcNewV7added inv1.5.0

func NewV7() (UUID,error)

UUID version 7 features a time-ordered value field derived from the widelyimplemented and well known Unix Epoch timestamp source,the number of milliseconds seconds since midnight 1 Jan 1970 UTC, leap seconds excluded.As well as improved entropy characteristics over versions 1 or 6.

seehttps://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-03#name-uuid-version-7

Implementations SHOULD utilize UUID version 7 over UUID version 1 and 6 if possible.

NewV7 returns a Version 7 UUID based on the current time(Unix Epoch).Uses the randomness pool if it was enabled with EnableRandPool.On error, NewV7 returns Nil and an error

funcNewV7FromReaderadded inv1.5.0

func NewV7FromReader(rio.Reader) (UUID,error)

NewV7FromReader returns a Version 7 UUID based on the current time(Unix Epoch).it use NewRandomFromReader fill random bits.On error, NewV7FromReader returns Nil and an error.

funcParse

func Parse(sstring) (UUID,error)

Parse decodes s into a UUID or returns an error if it cannot be parsed. Boththe standard UUID forms defined inRFC 4122(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx andurn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) are decoded. In addition,Parse accepts non-standard strings such as the raw hex encodingxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx and 38 byte "Microsoft style" encodings,e.g. {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}. Only the middle 36 bytes areexamined in the latter case. Parse should not be used to validate strings asit parses non-standard encodings as indicated above.

funcParseBytes

func ParseBytes(b []byte) (UUID,error)

ParseBytes is like Parse, except it parses a byte slice instead of a string.

func (UUID)ClockSequence

func (uuidUUID) ClockSequence()int

ClockSequence returns the clock sequence encoded in uuid.The clock sequence is only well defined for version 1 and 2 UUIDs.

func (UUID)Domain

func (uuidUUID) Domain()Domain

Domain returns the domain for a Version 2 UUID. Domains are only definedfor Version 2 UUIDs.

func (UUID)ID

func (uuidUUID) ID()uint32

ID returns the id for a Version 2 UUID. IDs are only defined for Version 2UUIDs.

func (UUID)MarshalBinary

func (uuidUUID) MarshalBinary() ([]byte,error)

MarshalBinary implements encoding.BinaryMarshaler.

func (UUID)MarshalText

func (uuidUUID) MarshalText() ([]byte,error)

MarshalText implements encoding.TextMarshaler.

func (UUID)NodeID

func (uuidUUID) NodeID() []byte

NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid isnot valid. The NodeID is only well defined for version 1 and 2 UUIDs.

func (*UUID)Scan

func (uuid *UUID) Scan(src interface{})error

Scan implements sql.Scanner so UUIDs can be read from databases transparently.Currently, database types that map to string and []byte are supported. Pleaseconsult database-specific driver documentation for matching types.

func (UUID)String

func (uuidUUID) String()string

String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.

func (UUID)Time

func (uuidUUID) Time()Time

Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded inuuid. The time is only defined for version 1, 2, 6 and 7 UUIDs.

func (UUID)URN

func (uuidUUID) URN()string

URN returns theRFC 2141 URN form of uuid,urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.

func (*UUID)UnmarshalBinary

func (uuid *UUID) UnmarshalBinary(data []byte)error

UnmarshalBinary implements encoding.BinaryUnmarshaler.

func (*UUID)UnmarshalText

func (uuid *UUID) UnmarshalText(data []byte)error

UnmarshalText implements encoding.TextUnmarshaler.

func (UUID)Value

func (uuidUUID) Value() (driver.Value,error)

Value implements sql.Valuer so that UUIDs can be written to databasestransparently. Currently, UUIDs map to strings. Please consultdatabase-specific driver documentation for matching types.

func (UUID)Variant

func (uuidUUID) Variant()Variant

Variant returns the variant encoded in uuid.

func (UUID)Version

func (uuidUUID) Version()Version

Version returns the version of uuid.

typeUUIDsadded inv1.4.0

type UUIDs []UUID

UUIDs is a slice of UUID types.

func (UUIDs)Stringsadded inv1.4.0

func (uuidsUUIDs) Strings() []string

Strings returns a string slice containing the string form of each UUID in uuids.

typeVariant

type Variantbyte

A Variant represents a UUID's variant.

func (Variant)String

func (vVariant) String()string

typeVersion

type Versionbyte

A Version represents a UUID's version.

func (Version)String

func (vVersion) String()string

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp