nanoid

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2024 License: GPL-3.0 Imports: 5 Imported by: 73

README

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidLength = errors.New("nanoid: length for ID is invalid (must be within 2-255)")

Functions

func ASCII added in v1.2.0

func ASCII(length int) (generator, error)

Returns a mutexed buffereed NanoID generator that uses an alphabet of ASCII characters 40-126 inclusive.

Errors if length is not within 2-255 (incl).

func Canonic added in v1.4.0

func Canonic() (generator, error)

Returns a standard NanoID generator with canonic length (21)

i.e., nanoid.Standard(21)

func Custom

func Custom(alphabet string, length int) (generator, error)

Deprecated; use nanoid.CustomUnicode.

func CustomASCII added in v1.1.0

func CustomASCII(alphabet string, length int) (generator, error)

Returns a Nano ID generator which uses a custom ASCII alphabet.

Uses less memory than CustomUnicode by only supporting ASCII. For unicode support use nanoid.CustomUnicode.

Errors if alphabet is not valid ASCII or if length is not within 2-255 (incl).

func CustomUnicode added in v1.1.0

func CustomUnicode(alphabet string, length int) (generator, error)

Returns a mutexed buffered NanoID generator which uses a custom alphabet that can contain non-ASCII (unicode).

Uses more memory by supporting unicode. For ASCII-only, use nanoid.CustomASCII.

🟡 Errors if length is within 2-255 (incl).

func MustCustomASCII added in v1.4.0

func MustCustomASCII(alphabet string, length int) generator

MustCustomASCII is a wrapper around CustomASCII but panics if any initialization error occurs.

func Standard

func Standard(length int) (generator, error)

Returns a mutexed buffered NanoID generator.

Errors if length is not within 2-255 (incl).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL