nanoid

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2023 License: Apache-2.0 Imports: 3 Imported by: 4

README

NanoID

NanoID is a lightweight Go package for generating short, unique IDs. It's a great alternative to longer, more complex UUIDs, especially when you need something more user-friendly.

Installation

go get github.com/9ssi7/nanoid

Usage

import "github.com/9ssi7/nanoid"

id, err := nanoid.New()
Custom Length ID
id, err := nanoid.NewWithLength(nanoid.LengthMedium)
if err != nil {
 log.Fatal(err)
}
Validating ID
if !nanoid.Validate(id) {
 log.Fatal("Invalid ID")
}

Benchmark

NanoID is highly performant and compares favorably with Google's UUID package.

goos: linux
goarch: amd64
pkg: github.com/9ssi7/nanoid
cpu: 12th Gen Intel(R) Core(TM) i7-12700H
BenchmarkNanoID
BenchmarkNanoID-20          1240796        933.8 ns/op
BenchmarkGoogleUUID
BenchmarkGoogleUUID-20      1312452        903.1 ns/op

As seen in the benchmark results, NanoID's performance is very close to Google UUID, making it a suitable choice for most use cases where short IDs are preferred.

Security

NanoID uses crypto/rand for generating secure random IDs, reducing the likelihood of collision. However, it's crucial to consider the trade-off between ID length and collision probability. Shorter IDs are more user-friendly but have a higher chance of collision, especially in systems with a high number of generated IDs.

Documentation

Overview

Package nanoid provides a simple API to generate short, unique IDs.

Installation:

go get github.com/9ssi7/nanoid

Usage:

import "github.com/9ssi7/nanoid"

id, err := nanoid.New()
if err != nil {
	log.Fatal(err)
}

Custom Length ID:

id, err := nanoid.NewWithLength(nanoid.LengthMedium)
if err != nil {
	log.Fatal(err)
}

Validating ID:

if !nanoid.Validate(id) {
	log.Fatal("Invalid ID")
}

Benchmark Results:

goos: linux
goarch: amd64
pkg: github.com/9ssi7/nanoid
cpu: 12th Gen Intel(R) Core(TM) i7-12700H
BenchmarkNanoID
BenchmarkNanoID-20        	 1240796	       933.8 ns/op
BenchmarkGoogleUUID
BenchmarkGoogleUUID-20    	 1312452	       903.1 ns/op

Security:

NanoID uses crypto/rand for generating secure random IDs, reducing the likelihood of collision. However, the users should consider the trade-off between ID length and collision probability.

License:

Apache-2.0 License

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustNew

func MustNew() string

MustNew returns a new random ID. It panics if any error occurs.

func New

func New() (string, error)

New returns a new random ID.

func NewWithLength

func NewWithLength(length Length) (string, error)

NewWithLength returns a new random ID with custom length.

func Validate

func Validate(id string) bool

Validate returns true if the given ID is valid.

Types

type Length

type Length int
const (
	LengthDefault Length = 21
	LengthShort   Length = 5
	LengthMedium  Length = 10
	LengthLong    Length = 15
)

Jump to

Keyboard shortcuts

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