Documentation
¶
Overview ¶
Package guuid provides a lightweight and efficient implementation of Universally Unique Identifiers (UUIDs) in Go, with primary support for UUIDv7.
UUIDv7 is a time-ordered UUID that combines the benefits of time-based ordering with cryptographic randomness. Unlike traditional UUIDs, UUIDv7 generates identifiers that are naturally sortable by creation time, making them ideal for:
- Database primary keys (improved B-tree performance)
- Distributed systems requiring time-ordered identifiers
- Event sourcing and audit logs
- Any scenario where chronological ordering matters
Basic Usage:
// Generate a new UUIDv7
id, err := guuid.New()
if err != nil {
log.Fatal(err)
}
fmt.Println(id.String())
// Parse a UUID from string
id, err := guuid.Parse("f47ac10b-58cc-4372-a567-0e02b2c3d479")
if err != nil {
log.Fatal(err)
}
// Get timestamp from UUIDv7
timestamp := id.Timestamp()
time := id.Time()
Custom Generator:
// Create a custom generator for better performance in tight loops
gen := guuid.NewGenerator()
for i := 0; i < 1000; i++ {
id, err := gen.New()
if err != nil {
log.Fatal(err)
}
// Use id...
}
Thread Safety:
All operations are thread-safe. The default generator can be used concurrently from multiple goroutines without additional synchronization.
Standards Compliance:
This implementation follows RFC 4122 and RFC 9562 specifications for UUIDs. The UUIDv7 format includes:
- 48-bit timestamp (millisecond precision)
- 12-bit random data for sub-millisecond ordering
- 62-bit random data for uniqueness
- Version and variant bits as per RFC specification
Index ¶
- Variables
- type Generator
- type UUID
- func DecodeFromBase64(s string) (UUID, error)
- func DecodeFromBase64Std(s string) (UUID, error)
- func DecodeFromHex(s string) (UUID, error)
- func FromBytes(b []byte) (UUID, error)
- func Must(uuid UUID, err error) UUID
- func MustFromBytes(b []byte) UUID
- func MustParse(s string) UUID
- func New() (UUID, error)
- func NewV7() (UUID, error)
- func Parse(s string) (UUID, error)
- func (u UUID) Bytes() []byte
- func (u UUID) Compare(other UUID) int
- func (u UUID) EncodeToBase64() string
- func (u UUID) EncodeToBase64Std() string
- func (u UUID) EncodeToHex() string
- func (u UUID) Equal(other UUID) bool
- func (u UUID) IsNil() bool
- func (u UUID) MarshalBinary() ([]byte, error)
- func (u UUID) MarshalText() ([]byte, error)
- func (u *UUID) Scan(src interface{}) error
- func (u UUID) String() string
- func (u UUID) Time() time.Time
- func (u UUID) Timestamp() int64
- func (u *UUID) UnmarshalBinary(data []byte) error
- func (u *UUID) UnmarshalText(data []byte) error
- func (u UUID) Value() (driver.Value, error)
- func (u UUID) Variant() Variant
- func (u UUID) Version() Version
- type Variant
- type Version
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidFormat indicates that the UUID string format is invalid ErrInvalidFormat = errors.New("guuid: invalid UUID format") // ErrInvalidLength indicates that the UUID byte slice has incorrect length ErrInvalidLength = errors.New("guuid: invalid UUID length (expected 16 bytes)") // ErrInvalidVersion indicates that the UUID version is not supported ErrInvalidVersion = errors.New("guuid: invalid or unsupported UUID version") // ErrInvalidVariant indicates that the UUID variant is not RFC 4122 ErrInvalidVariant = errors.New("guuid: invalid UUID variant (expected RFC 4122)") )
Functions ¶
This section is empty.
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator is a thread-safe UUIDv7 generator that ensures monotonicity within the same millisecond by using a counter with random data.
func NewGenerator ¶
func NewGenerator() *Generator
NewGenerator creates a new UUIDv7 generator with crypto/rand as the random source
func NewGeneratorWithReader ¶
NewGeneratorWithReader creates a new UUIDv7 generator with a custom random source. This is primarily useful for testing with deterministic random sources.
type UUID ¶
type UUID [16]byte
UUID represents a Universally Unique Identifier as defined by RFC 4122 and RFC 9562. The UUID is a 128-bit (16 byte) value that is used to uniquely identify information.
var Nil UUID
Nil is the nil UUID (all zeros)
func DecodeFromBase64 ¶
DecodeFromBase64 decodes a base64 string to UUID (URL-safe encoding)
func DecodeFromBase64Std ¶
DecodeFromBase64Std decodes a standard base64 string to UUID
func DecodeFromHex ¶
DecodeFromHex decodes a hexadecimal string to UUID
func Must ¶
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 id = guuid.Must(generator.New())
func MustFromBytes ¶
MustFromBytes is like FromBytes but panics on error
func MustParse ¶
MustParse is like Parse but panics if the string cannot be parsed. It simplifies safe initialization of global variables.
func New ¶
New generates a new UUIDv7 using the default generator. This is a convenience function that uses the package-level generator.
func Parse ¶
Parse parses a UUID from its string representation. It accepts the following formats:
- xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (canonical)
- urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
- {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (without hyphens)
func (UUID) Compare ¶
Compare returns an integer comparing two UUIDs lexicographically. The result will be 0 if u==other, -1 if u < other, and +1 if u > other.
func (UUID) EncodeToBase64 ¶
EncodeToBase64 encodes the UUID to a base64 string (URL-safe, no padding)
func (UUID) EncodeToBase64Std ¶
EncodeToBase64Std encodes the UUID to a standard base64 string
func (UUID) EncodeToHex ¶
EncodeToHex encodes the UUID to a hexadecimal string without hyphens
func (UUID) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface
func (UUID) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface
func (UUID) String ¶
String returns the canonical string representation of the UUID in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
func (*UUID) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface
func (*UUID) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface