jaevor / go-nanoid

Nano ID for Go

Home Page:https://pkg.go.dev/github.com/jaevor/go-nanoid

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go-nanoid

Build Status Build Status GitHub Issues Go Version Go Ref

This module is a Go implementation of ai/nanoid.

go get github.com/jaevor/go-nanoid
import (
	"log"
	"github.com/jaevor/go-nanoid"
)

func main() {
	// The canonic NanoID is nanoid.Standard(21).
	canonicID, err := nanoid.Standard(21)
	if err != nil {
		panic(err)
	}

	id1 := canonicID()
	log.Printf("ID 1: %s", id1) // eLySUP3NTA48paA9mLK3V

	// Makes sense to use CustomASCII since 0-9 is ASCII.
	decenaryID, err := nanoid.CustomASCII("0123456789", 12)
	if err != nil {
		panic(err)
	}

	id2 := decenaryID()
	log.Printf("ID 2: %s", id2) // 817411560404
}

Note

This module's functions use an internal buffered slice of random bytes, and thus also a mutex. This slight overhead of memory allows it to be very efficient, but may not be ideal if you are only generating an ID every now and then.

Security

Benchmarks

All benchmarks & tests are in nanoid_test.go.

These are all benchmarks of nanoid.Standard(#)

# of characters & # of IDs benchmark screenshot
8, ~21,800,000
21, ~16,400,000
36, ~11,500,000
255, ~2,500,000

Credits & references

License

GNU General Public License v3.0

About

Nano ID for Go

https://pkg.go.dev/github.com/jaevor/go-nanoid

License:GNU General Public License v3.0


Languages

Language:Go 99.0%Language:Makefile 1.0%