nanoid

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 5 Imported by: 1

README

nanoid

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-Z a-z 0-9 _ -). So ID size was reduced from 36 to 21 symbols.

Not original work

Initially copied from: github.com/aidarkhanov/nanoid

License: original MIT license

Any errors due to changes or modifications to the original code is on me.

Usage


id := nanoid.WebSafeID()
// a 4 x 4 web safe id, first character is NOT a numeric, as html element IDs require
// e.g. aHd7-5gCW-8Hdt-Z9wh

Documentation

Index

Constants

View Source
const (
	// DefaultAlphabet is the default alphabet for Nano ID.
	DefaultAlphabet = "-_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	// Only Alpha. Used as the first charactor of WebSafe IDs,
	// as numbers as not allowed as the first character of an HTML element ID
	AlphaOnly = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	// No Punctuation, but all upper and lower alpha and all numeric characters
	AlphaNumeric = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	// 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.

func GenerateString

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

GenerateString generates a random string based on alphabet and size.

func New

func New() (string, error)

New generates a random string.

func New14

func New14() (string, error)

New generates a random string, but only 14 characters long.

func NewMust

func NewMust() string

for times when returning an error is not desired

func WebSafeID

func WebSafeID() string

WebSafeID, returns a 16 character (4x4, dash separated) string that is safe to use as an HTML ID

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