A golang implementation of TypeIDs
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.
To add this library as a dependency in your go module, run:
go get go.jetpack.io/typeid
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.