Giter Site home page Giter Site logo

formalco / typeid Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jetify-com/typeid-go

0.0 0.0 0.0 65 KB

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

License: Apache License 2.0

Go 100.00%

typeid's Introduction

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 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:

import (
  "go.jetpack.io/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:

import (
  "go.jetpack.io/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:

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

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

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

typeid's People

Contributors

loreto avatar mikeland73 avatar broothie avatar gcurtis avatar fernandezpablo85 avatar webrat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.