guid

package
v0.0.0-...-0941746 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package guid helps generate and parse GUIDs.

GUIDs are similar to UUIDs (RFC 4122) but use are encoded using base32 to be more user-friendly.

A GUID is a 16 byte (128 bit) array and is compatible with standard UUIDs which is handy if you need to interoperate with other systems. GUID are stored in databases as a 16 byte array and thus can leverage native UUID types for better performances.

go test -bench=.
pkg: git.sr.ht/~pingoo/stdx/guid
BenchmarkNewRandomPool/goroutines-4-4           12655129                94.50 ns/op
BenchmarkNewRandomPool/goroutines-40-4          12680683                93.89 ns/op
BenchmarkNewRandomPool/goroutines-2000-4        12629418                94.45 ns/op
BenchmarkNewRandomPool/goroutines-4000-4        12702556                94.73 ns/op
BenchmarkNewRandomPool/goroutines-8000-4        12450429                95.09 ns/op
BenchmarkNewRandomReader/goroutines-40-4         6811862               154.2 ns/op
BenchmarkNewRandomReader/goroutines-2000-4       6851259               165.7 ns/op
BenchmarkNewRandomReader/goroutines-4000-4       7351102               158.5 ns/op
BenchmarkNewRandomReader/goroutines-8000-4       7219173               154.8 ns/op
BenchmarkNewRandomReader/goroutines-40000-4      7054268               159.7 ns/op

Index

Constants

View Source
const (
	Size = 16
)

Variables

View Source
var (
	ErrGuidIsNotValid = errors.New("GUID is not valid")
	ErrUuidIsNotValid = errors.New("Not a valid UUID")
)

Functions

func SetPoolEnabled

func SetPoolEnabled(enabled bool)

func SetRand

func SetRand(r io.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 then a panic will be issued.

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

Types

type GUID

type GUID [Size]byte

A GUID is a 128 bit (16 byte) Globally Unique IDentifier

var (
	Empty GUID // empty GUID, all zeros

)

func FromBytes

func FromBytes(b []byte) (guid GUID, err error)

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

func NewRandom

func NewRandom() GUID

func NewRandomWithErr

func NewRandomWithErr() (GUID, error)

func NewTimeBased

func NewTimeBased() GUID

func NewTimeBasedWithErr

func NewTimeBasedWithErr() (guid GUID, err error)

TODO: Improve by reading only 10 bytes of random data

func Parse

func Parse(input string) (guid GUID, err error)

TODO: parse without allocs

func ParseUuidString

func ParseUuidString(input string) (guid GUID, err error)

Parse decodes s into a GUID as an UUID or returns an error. The form shoud be: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

func (GUID) Bytes

func (guid GUID) Bytes() []byte

func (GUID) Equal

func (guid GUID) Equal(other GUID) bool

func (GUID) MarshalBinary

func (guid GUID) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler.

func (GUID) MarshalText

func (guid GUID) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*GUID) Scan

func (guid *GUID) Scan(src interface{}) error

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

func (GUID) String

func (guid GUID) String() string

String returns the string form of guid TODO: encode without alloc

func (GUID) ToUuidString

func (guid GUID) ToUuidString() string

UUIDString returns the UUID string form of GUID, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

func (*GUID) UnmarshalBinary

func (guid *GUID) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler.

func (*GUID) UnmarshalText

func (guid *GUID) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (GUID) Value

func (guid GUID) Value() (driver.Value, error)

Value implements sql.Valuer so that GUIDs can be written to databases transparently. Currently, GUIDs map to strings. Please consult database-specific driver documentation for matching types.

Jump to

Keyboard shortcuts

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