Skip to content

Go implementation of TypeIDs: type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs

License

Notifications You must be signed in to change notification settings

webRat/typeid-go

 
 

Repository files navigation

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/typeid"
)

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

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

About

Go implementation of TypeIDs: type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%