uuidvii

package module
v0.0.0-...-a56bf27 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2023 License: MIT Imports: 7 Imported by: 0

README

UUIDv7 in Go

Go Reference

Usage

import (
    "fmt"

    "github.com/enzzc/uuidvii"
)

func main() {
    uuidSource := uuidvii.NewUUIDSource()
    fmt.Println(uuidSource.New().Hex())
}

Output:

176f1466-2d3f-70ba-8455-ef4a309ce2a8

Features

  • Tend to be RFC4122-compliant
  • Use cryptographically secure random numbers with AES, which is efficient (hardware-accelerated on x86_64 and modern AArch64 CPUs.)
  • Time-ordered and continually increasing even when wall-clock changes
  • Zero-dependency
  • Tested on x86_64 and Arm64 (AArch64).

Performance

Here is what I get ("Performance" mode enabled, AES-NI supported):

goos: linux
goarch: amd64
pkg: github.com/enzzc/uuidvii
cpu: 12th Gen Intel(R) Core(TM) i7-1255U
BenchmarkGenUUID7-12         	16237479	        77.12 ns/op
BenchmarkGenUUID7AsHex-12    	10543224	       111.8 ns/op
PASS
ok  	github.com/enzzc/uuidvii	3.215s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type UUIDSource

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

UUIDSource represents a UUIDv7 generator. Subsequent UUIDs generated by the same UUIDSource are guaranteed to be ordered (esp. time-ordered). Most likely you need one UUIDSource per application. UUIDSource is thread-safe.

func NewUUIDSource

func NewUUIDSource() *UUIDSource

NewUUIDSource initializes and returns a new UUIDSource which is ready to use.

func (*UUIDSource) New

func (u *UUIDSource) New() UUIDv7

New generates and returns a new UUIDv7.

func (*UUIDSource) NewAsV4

func (u *UUIDSource) NewAsV4() UUIDv7

NewAsV4 returns a new UUIDv7 with the version bits set to 0x4 for systems expecting only valid UUIDv4.

type UUIDv7

type UUIDv7 []byte

UUIDv7 represents a UUID version 7 as defined in the RFC4122. See: <https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis#name-uuid-version-7>.

func (UUIDv7) Hex

func (u UUIDv7) Hex() string

Hex returns the hexadecimal (with dashes) representation of a UUIDv7. Hex is optimized and should be prefered over the String method.

func (UUIDv7) String

func (u UUIDv7) String() string

String returns a human-friendly hexadecimal representation of a UUIDv7.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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