cuid

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2021 License: MIT Imports: 13 Imported by: 120

README

cuid

Collision-resistant hashes for the cloud, in Go.

The cuid package provides collision-resistant ids optimized for horizontal scaling and sequential lookup performance. This README file is just going to cover the basics and the Go-specific implementation details.

Please refer to the main project site for the full rationale behind CUIDs.

Sample CUID

ch72gsb320000udocl363eofy

Sample CUID Slug

ew0k9fwpl

Installation

go get -u github.com/lucsky/cuid

Example usage

package main

import (
    "crypto/rand"
    "fmt"

    "github.com/lucsky/cuid"
)

func main() {
    // Generate pseudo-random CUID
    fmt.Println(cuid.New())
    // Generate slug
    fmt.Println(cuid.Slug())

    // Generate cryptographic-random CUID
    c, err := cuid.NewCrypto(rand.Reader)
    if err != nil {
        fmt.Printf("%v", err)
        return
    }
    fmt.Println(c)
}

Go package specific features

The Go cuid package provides APIs to specify a custom random source as well as a custom counter. A custom counter implementation could provide a centralized Redis base counter, for example.

Contributors

  • Luc Heinrich (@lucsky, author)
  • Thomas Hopkins (@hopkinsth)
  • fiatjaf
  • Marcus Dantas (@mpsdantas)
  • Oscar Forner Martinez (@maitesin)
  • Mike Frey (@mikefrey)
  • Andris Mednis (@andrismednis)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsCuid added in v1.1.0

func IsCuid(c string) error

func IsSlug added in v1.1.0

func IsSlug(s string) error

func New

func New() string

func NewCrypto added in v1.2.0

func NewCrypto(reader io.Reader) (string, error)

func SetCounter

func SetCounter(cnt Counter)

func SetRandom

func SetRandom(rnd *rand.Rand)

func SetRandomSource

func SetRandomSource(src rand.Source)

func Slug added in v1.0.2

func Slug() string

Types

type Counter

type Counter interface {
	Next() int32
}

type DefaultCounter

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

func (*DefaultCounter) Next

func (c *DefaultCounter) Next() int32

Jump to

Keyboard shortcuts

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