shortuuid

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2015 License: MIT Imports: 6 Imported by: 0

README

shortuuid

Build Status Godoc

A Go library that generates concise, unambiguous, URL-safe UUIDs. Based on and compatible with the Python library shortuuid.

Often, one needs to use non-sequential IDs in places where users will see them, but the IDs must be as concise and easy to use as possible. shortuuid solves this problem by generating UUIDs using satori/go.uuid and then translating them to base57 using lowercase and uppercase letters and digits, and removing similar-looking characters such as l, 1, I, O and 0.

Usage

package main

import (
    "fmt"

    "github.com/renstrom/shortuuid"
)

func main() {
    id := shortuuid.UUID()  // "ajLWxEodc6CmQLHADuKVwD"

    u := shortuuid.New()
    fmt.Printf("%s", u)     // Cekw67uyMpBGZLRP2HFVbe
    u.UUID("")              // Generate a new UUID
    fmt.Printf("%s", u)     // 4pUYNRFHTG3YVgThPZvCgC
}

To use UUID v5 (instead of the default v4), pass a namespace (DNS or URL) to the .UUID(name string) call:

shortuuid.New().UUID("http://example.com")

License

MIT

Documentation

Index

Constants

View Source
const DefaultAlphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

DefaultAlphabet is the default alphabet used.

Variables

This section is empty.

Functions

func UUID

func UUID() string

UUID returns a new UUID v4.

Types

type ShortUUID

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

ShortUUID represents a short UUID encoder.

func New

func New() *ShortUUID

New returns a new (short) UUID with the default alphabet.

func NewWithAlphabet

func NewWithAlphabet(alphabet string) (*ShortUUID, error)

NewWithAlphabet returns a new (short) UUID using alphabet. The alphabet will be sorted and if it contains duplicate characters, they will be removed.

func (*ShortUUID) Decode

func (su *ShortUUID) Decode(u string) (uuid.UUID, error)

Decode decodes a string according to the alphabet into a uuid.UUID. If s is too short, its most significant bits (MSB) will be padded with 0 (zero).

func (*ShortUUID) Encode

func (su *ShortUUID) Encode(u uuid.UUID) string

Encode encodes uuid.UUID into a string using the least significant bits (LSB) first according to the alphabet. if the most significant bits (MSB) are 0, the string might be shorter.

func (*ShortUUID) String

func (su *ShortUUID) String() string

String returns a (short) UUID v4, and will generate one if necessary. To generate a new string, use UUID(). Implements the fmt.Stringer interface.

func (*ShortUUID) UUID

func (su *ShortUUID) UUID(name string) string

UUID returns a new (short) UUID. If name is non-empty, the namespace matching the name will be used to generate a UUID.

Jump to

Keyboard shortcuts

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