nanoid

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2020 License: MIT Imports: 4 Imported by: 18

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 := nanoid.New() //> "i25_rX9zwDdDn7Sg-ZoaH"
Installation

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

go get github.com/aidarkhanov/nanoid
Documentation

The package reference is located at godoc.org/github.com/aidarkhanov/nanoid.

Roadmap
  • Remove panic handling from the API.
  • Freeze the API in version 2 of the package.
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

This section is empty.

Variables

This section is empty.

Functions

func Format

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

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

Example
package main

import (
	"crypto/rand"
	"fmt"

	"github.com/aidarkhanov/nanoid"
)

func main() {
	alphabet := "-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW"
	size := 21

	// 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.Format(generateBytesBuffer, alphabet, size)
	if err != nil {
		panic(err)
	}

	fmt.Println(id)
}
Output:

func Generate

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

Generate generates a random string based on alphabet and size.

Example
package main

import (
	"fmt"

	"github.com/aidarkhanov/nanoid"
)

func main() {
	alphabet := "-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW"
	size := 21

	id, err := nanoid.Generate(alphabet, size)
	if err != nil {
		panic(err)
	}

	fmt.Println(id)
}
Output:

func Must

func Must(id string, err error) string

Must returns a random string if err is nil or panics otherwise.

func MustFormat

func MustFormat(generateRandomBuffer BytesGenerator, alphabet string, size int) string

MustFormat is like Format but panics if a random string cannot be generated.

Example
package main

import (
	"crypto/rand"
	"fmt"

	"github.com/aidarkhanov/nanoid"
)

func main() {
	alphabet := "-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW"
	size := 21

	// 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 := nanoid.MustFormat(generateBytesBuffer, alphabet, size)

	fmt.Println(id)
}
Output:

func MustGenerate

func MustGenerate(alphabet string, size int) string

MustGenerate is like Generate but panics if a random string cannot be generated.

Example
package main

import (
	"fmt"

	"github.com/aidarkhanov/nanoid"
)

func main() {
	alphabet := "-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW"
	size := 21

	id := nanoid.MustGenerate(alphabet, size)

	fmt.Println(id)
}
Output:

func New

func New() string

New generates a random string.

Example
package main

import (
	"fmt"

	"github.com/aidarkhanov/nanoid"
)

func main() {
	id := nanoid.New()

	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