nanoid

package module
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2020 License: MIT Imports: 4 Imported by: 22

README

Nano ID GoDoc

A tiny and fast Go unique string generator
  • Safe. It uses cryptographically strong random APIs and tests distribution of symbols.
  • Compact. It uses a larger alphabet than UUID (A-Za-z0-9_-). So ID size was reduced from 36 to 21 symbols.
id, err := nanoid.New() //> "i25_rX9zwDdDn7Sg-ZoaH"
if err != nil {
    log.Fatalln(err)
}
Installation

Once Go is installed, run the following command to get Nano ID.

go get github.com/aidarkhanov/nanoid/v2
Documentation

The package reference is located at pkg.go.dev/github.com/aidarkhanov/nanoid/v2.

Roadmap
  • The API of this package is frozen.
  • Release patches if necessary.
License

This package is provided under MIT/Expat license. See LICENSE.md file for details.

Thanks to

Documentation

Overview

Package nanoid provides fast and convenient unique string generator.

Index

Examples

Constants

View Source
const (
	// DefaultAlphabet is the default alphabet for Nano ID.
	DefaultAlphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
	// DefaultSize is the default size for Nano ID.
	DefaultSize = 21
)

Variables

This section is empty.

Functions

func FormatString

func FormatString(generateRandomBuffer BytesGenerator, alphabet string, size int) (string, error)

FormatString generates a random string based on BytesGenerator, alphabet and size.

Example
alphabet := nanoid.DefaultAlphabet
size := nanoid.DefaultSize

// generateBytesBuffer returns random bytes buffer
generateBytesBuffer := func(step int) ([]byte, error) {
	buffer := make([]byte, step)
	if _, err := rand.Read(buffer); err != nil {
		return nil, err
	}

	return buffer, nil
}

id, err := nanoid.FormatString(generateBytesBuffer, alphabet, size)
if err != nil {
	log.Fatalln(err)
}

fmt.Println(id)
Output:

func GenerateString

func GenerateString(alphabet string, size int) (string, error)

GenerateString generates a random string based on alphabet and size.

Example
alphabet := nanoid.DefaultAlphabet
size := nanoid.DefaultSize

id, err := nanoid.GenerateString(alphabet, size)
if err != nil {
	log.Fatalln(err)
}

fmt.Println(id)
Output:

func New

func New() (string, error)

New generates a random string.

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

fmt.Println(id)
Output:

Types

type BytesGenerator

type BytesGenerator func(step int) ([]byte, error)

BytesGenerator represents random bytes buffer.

Jump to

Keyboard shortcuts

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