Categorygo.jetpack.io/typeid
modulepackage
0.1.0
Repository: https://github.com/jetify-com/typeid-go.git
Documentation: pkg.go.dev

# README

TypeID Go

A golang implementation of TypeIDs

License: Apache 2.0 Go Reference

TypeIDs are a modern, type-safe, globally unique identifier based on the upcoming UUIDv7 standard. They provide a ton of nice properties that make them a great choice as the primary identifiers for your data in a database, APIs, and distributed systems. Read more about TypeIDs in their spec.

This particular implementation provides a go library for generating and parsing TypeIDs.

Installation

To add this library as a dependency in your go module, run:

go get go.jetpack.io/typeid

Usage

This library provides both a statically typed and a dynamically typed version of TypeIDs.

The statically typed version lives under the typed package. It makes it possible for the go compiler itself to enforce type safety.

To use it, first define your TypeID types:

import (
  typeid "go.jetpack.io/typeid/typed"
)

type userPrefix struct{}
func (userPrefix) Type() string { return "user" }
type UserID struct { typeid.TypeID[userPrefix] }

And now use those types to generate TypeIDs:

import (
  typeid "go.jetpack.io/typeid/typed"
)

func example() {
  tid, _ := typeid.New[UserID]()
  fmt.Println(tid)
}

If you don't want static types, you can use the dynamic version instead:

import (
  "go.jetpack.io/typeid"
)

func example() {
  tid, _ := typeid.New("user")
  fmt.Println(tid)
}

For the full documentation, see this package's godoc.

# Packages

No description provided by the author
Package typed implements a statically checked version of TypeIDs To use it, define your own ID types that implement the IDType interface: type userPrefix struct{} func (userPrefix) Type() string { return "user" } type UserID struct { typeid.TypeID[userPrefix] } type accountPrefix struct{} func (accountPrefix) Type() string { return "account" } type AccountID struct { typeid.TypeID[accountPrefix] } And now you can use your IDTypes via generics.

# Functions

From returns a new TypeID with the given prefix and suffix.
FromString parses a TypeID from a string of the form <prefix>_<suffix>.
FromUUID encodes the given UUID (in hex string form) as a TypeID with the given prefix.
FromUUID encodes the given UUID (in byte form) as a TypeID with the given prefix.
Must returns a TypeID if the error is nil, otherwise panics.
New returns a new TypeID with the given prefix and a random suffix.

# Variables

Nil represents an the null TypeID.

# Structs

TypeID is a unique identifier with a given type as defined by the TypeID spec.