https://github.com/jetify-com/typeid-go
Go implementation of TypeIDs: type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs
https://github.com/jetify-com/typeid-go
go golang guid typeid uuid uuidv7
Last synced: 21 days ago
JSON representation
Go implementation of TypeIDs: type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs
- Host: GitHub
- URL: https://github.com/jetify-com/typeid-go
- Owner: jetify-com
- License: apache-2.0
- Created: 2023-06-07T21:36:32.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-06T20:24:53.000Z (about 2 months ago)
- Last Synced: 2025-03-29T17:05:38.859Z (28 days ago)
- Topics: go, golang, guid, typeid, uuid, uuidv7
- Language: Go
- Homepage:
- Size: 68.4 KB
- Stars: 145
- Watchers: 8
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# TypeID Go
### A golang implementation of [TypeIDs](https://github.com/jetify-com/typeid)
 [](https://pkg.go.dev/go.jetify.com/typeid)
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](https://github.com/jetify-com/typeid).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:
```bash
go get go.jetify.com/typeid
```## Usage
This library provides a go implementation of TypeID that allows you
to define your own custom id types for added compile-time safety.If you don't need compile-time safety, you can use the provided `typeid.AnyID` directly:
```go
import (
"go.jetify.com/typeid"
)func example() {
tid, _ := typeid.WithPrefix("user")
fmt.Println(tid)
}
```If you want compile-time safety, define your own custom types with two steps:
1. Define a struct the implements the method `Prefix`. Prefix should return the
string that should be used as the prefix for your custom type.
2. Define you own id type, by embedding `typeid.TypeID[CustomPrefix]`For example to define a UserID with prefix `user`:
```go
import (
"go.jetify.com/typeid"
)// Define the prefix:
type UserPrefix struct {}
func (UserPrefix) Prefix() string { return "user" }// Define UserID:
type UserID struct {
typeid.TypeID[UserPrefix]
}
```Now you can use the UserID type to generate new ids:
```go
import (
"go.jetify.com/typeid"
)func example() {
tid, _ := typeid.New[UserID]()
fmt.Println(tid)
}
```For the full documentation, see this package's [godoc](https://pkg.go.dev/go.jetify.com/typeid).