nanoid

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: MIT Imports: 4 Imported by: 1

README

NanoID

Go Reference Go Report Card

A simple, fast, and efficient Go implementation of NanoID, a tiny, secure, URL-friendly, unique string ID generator.

Features

  • Secure: Uses crypto/rand for cryptographically secure random number generation.
  • Fast: Optimized for performance with efficient algorithms.
  • Thread-Safe: Safe for concurrent use in multi-threaded applications.
  • Customizable: Specify custom ID lengths and alphabets.
  • Easy to Use: Simple API with sensible defaults.

Installation

To install the package, use:

go get -u github.com/sixafter/nanoid

Importing the Package

To use the NanoID package in your Go project, import it as follows:

import "github.com/sixafter/nanoid"

Usage

Generating a Default NanoID

Generate a NanoID using the default size (21 characters) and the default alphabet (numbers and uppercase/lowercase letters):

package main

import (
    "fmt"
    "log"

    "github.com/sixafter/nanoid"
)

func main() {
    id, err := nanoid.Generate()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Generated NanoID:", id)
}
Generating a NanoID with Custom Size

Generate a NanoID with a custom length:

package main

import (
    "fmt"
    "log"

    "github.com/sixafter/nanoid"
)

func main() {
    id, err := nanoid.GenerateSize(10) // Generate a 10-character NanoID
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("NanoID with custom size:", id)
}
Generating a NanoID with Custom Alphabet

Generate a NanoID with a custom length and a custom set of characters:

package main

import (
    "fmt"
    "log"

    "github.com/sixafter/nanoid"
)

func main() {
    alphabet := "0123456789abcdef" // Hexadecimal characters
    id, err := nanoid.GenerateCustom(16, alphabet) // Generate a 16-character NanoID
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("NanoID with custom alphabet:", id)
}
Concurrency and Thread Safety

The NanoID functions are designed to be thread-safe. You can safely generate IDs from multiple goroutines concurrently without additional synchronization.

package main

import (
    "fmt"
    "sync"

    "github.com/sixafter/nanoid"
)

func main() {
    var wg sync.WaitGroup
    for i := 0; i < 10; i++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            id, err := nanoid.Generate()
            if err != nil {
                fmt.Println("Error generating NanoID:", err)
                return
            }
            fmt.Println("Generated NanoID:", id)
        }()
    }
    wg.Wait()
}
Error Handling

All functions return an error as the second return value. Ensure you handle any potential errors:

id, err := nanoid.Generate()
if err != nil {
    // Handle the error
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate() (string, error)

Generate generates a NanoID with the default size and alphabet.

func GenerateCustom

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

GenerateCustom generates a NanoID with a specified size and custom alphabet.

func GenerateSize

func GenerateSize(size int) (string, error)

GenerateSize generates a NanoID with a specified size and the default alphabet.

Types

This section is empty.

Jump to

Keyboard shortcuts

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