nanoid

package module
v0.0.0-...-262b0bd Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2024 License: MIT Imports: 5 Imported by: 0

README

NanoID

NanoID is a lightweight/developer-friendly Go library for generating unique IDs.

Installation

To use NanoID in your Go project, simply run:

go get github.com/ephemeralforge/nanoid

Usage

package main

import (
	"fmt"
	"github.com/ephemeralforge/nanoid"
)

func main() {
	id, err := nanoid.New()
	if err != nil {
		fmt.Println("Error generating NanoID:", err)
		return
	}
	fmt.Println("Generated NanoID:", id.String())
}

Documentation

Index

Constants

View Source
const (
	CanonicNanoIDLenght   = 21
	CanonicNanoIDAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
)

Variables

View Source
var (
	ErrInvalidBufferRead = errors.New("there is a problem reading random buffer with cripto/rand")
	ErrInvalidIDLength   = errors.New("the id length cannot be less than 2 or greater than 255")
	ErrNilAlphabet       = errors.New("the alphabet cannot be nil")
)

Functions

func CanonicNanoIDRandomFunc

func CanonicNanoIDRandomFunc(b []byte) error

func WithAlphabet

func WithAlphabet(alphabet Alphabet) func(*Option) *Option

func WithLength

func WithLength(length int) func(*Option) *Option

func WithRandomFunc

func WithRandomFunc(randomFunc func([]byte) error) func(*Option) *Option

Types

type Alphabet

type Alphabet []rune

func AlphabetFromString

func AlphabetFromString(s string) Alphabet

type NanoID

type NanoID []rune

func New

func New(options ...func(*Option) *Option) (NanoID, error)

TODO: buffers optimization New NanoID with options or canonnic by default.

func Parse

func Parse[T any](_ T, _ ...func(*Option) *Option) (NanoID, error)

func ParseFromString

func ParseFromString(_ string, _ ...func(*Option) *Option) (NanoID, error)

func (NanoID) String

func (n NanoID) String() string

type Option

type Option struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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