guid

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	V1 byte
	V2
	V3
	V4
	V5
)

UUID versions

View Source
const (
	VariantNCS byte = iota
	VariantRFC4122
	VariantMicrosoft
	VariantFuture
)

UUID layout variants.

View Source
const (
	DomainPerson = iota
	DomainGroup
	DomainOrg
)

UUID DCE domains.

View Source
const Size = 16

Size of a UUID in bytes.

Variables

View Source
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.

View Source
var Nil = UUID{}

Functions

func FastUuid

func FastUuid() string

func GenerateNanoId

func GenerateNanoId(alphabet string, size int) (string, error)

GenerateNanoId 使用初始字符表和长度生成ID

func MustGenerateNanoId

func MustGenerateNanoId(alphabet string, size int) string

MustGenerateNanoId is the same as Generate but panics on error.

func MustNanoId

func MustNanoId(l ...int) string

MustNanoId is the same as New but panics on error.

func NewNanoId

func NewNanoId(l ...int) (string, error)

NewNanoId generates secure URL-friendly unique ID. Accepts optional parameter - length of the ID to be generated (21 by default).

func Uuid1

func Uuid1() string

func Uuid2

func Uuid2() string

func Uuid3

func Uuid3(name string) string

func Uuid4

func Uuid4() string

func Uuid5

func Uuid5(name string) string

Types

type Generator

type Generator interface {
	NewV1() (UUID, error)
	NewV2(domain byte) (UUID, error)
	NewV3(ns UUID, name string) UUID
	NewV4() (UUID, error)
	NewV5(ns UUID, name string) UUID
}

Generator provides interface for generating UUIDs.

type Snowflake

type Snowflake struct {
	// contains filtered or unexported fields
}

func NewSnowflake

func NewSnowflake(machineID int64) (*Snowflake, error)

func (*Snowflake) NextId

func (s *Snowflake) NextId() int64

type UUID

type UUID [Size]byte

func FromBytes

func FromBytes(input []byte) (u UUID, err error)

FromBytes returns UUID converted from raw byte slice input. It will return error if the slice isn't 16 bytes long.

func FromBytesOrNil

func FromBytesOrNil(input []byte) UUID

FromBytesOrNil returns UUID converted from raw byte slice input. Same behavior as FromBytes, but returns a Nil UUID on error.

func FromString

func FromString(input string) (u UUID, err error)

FromString returns UUID parsed from string input. Input is expected in a form accepted by UnmarshalText.

func FromStringOrNil

func FromStringOrNil(input string) UUID

FromStringOrNil returns UUID parsed from string input. Same behavior as FromString, but returns a Nil UUID on error.

func Must

func Must(u UUID, err error) UUID

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"));

func NewV1

func NewV1() (UUID, error)

NewV1 基于timestamp和MAC address(RFC 4122)

func NewV2

func NewV2(domain byte) (UUID, error)

NewV2 基于timestamp, MAC address和POSIX UID/GID (DCE 1.1)

func NewV3

func NewV3(ns UUID, name string) UUID

NewV3 基于MD5 hashing(RFC 4122)

func NewV4

func NewV4() (UUID, error)

NewV4 基于random numbers(RFC 4122)

func NewV5

func NewV5(ns UUID, name string) UUID

NewV5 基于SHA-1 hashing(RFC 4122)

func (UUID) Bytes

func (u UUID) Bytes() []byte

Bytes returns bytes slice representation of UUID.

func (UUID) MarshalBinary

func (u UUID) MarshalBinary() (data []byte, err error)

MarshalBinary 序列化为字节数组

func (UUID) MarshalText

func (u UUID) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by String.

func (UUID) SetVariant

func (u UUID) SetVariant(v byte)

SetVariant sets variant bits.

func (UUID) SetVersion

func (u UUID) SetVersion(v byte)

SetVersion sets version bits.

func (UUID) String

func (u UUID) String() string

Returns canonical string representation of UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.

func (UUID) UnmarshalBinary

func (u UUID) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. 字节数组的长度不是16位,则返回错误

func (UUID) UnmarshalText

func (u UUID) UnmarshalText(text []byte) (err error)

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"

ABNF for supported UUID text representation follows:

uuid := canonical | hashlike | braced | urn
plain := canonical | hashlike
canonical := 4hexoct '-' 2hexoct '-' 2hexoct '-' 6hexoct
hashlike := 12hexoct
braced := '{' plain '}'
urn := URN ':' UUID-NID ':' plain
URN := 'urn'
UUID-NID := 'uuid'
12hexoct := 6hexoct 6hexoct
6hexoct := 4hexoct 2hexoct
4hexoct := 2hexoct 2hexoct
2hexoct := hexoct hexoct
hexoct := hexdig hexdig
hexdig := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' |
          'a' | 'b' | 'c' | 'd' | 'e' | 'f' |
          'A' | 'B' | 'C' | 'D' | 'E' | 'F'

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL