A generator library for concise, unambiguous and URL-safe UUIDs

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 google/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/lithammer/shortuuid/v4"
)

func main() {
	u := shortuuid.New() // KwSysDpxcBU9FNhGkn2dCf
}

To use UUID v5 (instead of the default v4), use NewWithNamespace(name string) instead of New().

shortuuid.NewWithNamespace("http://example.com")

It's possible to use a custom alphabet as well, though it has to be 57 characters long.

alphabet := "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxy="
shortuuid.NewWithAlphabet(alphabet) // iZsai==fWebXd5rLRWFB=u

Bring your own encoder! For example, base58 is popular among bitcoin.

package main

import (
	"fmt"

	"github.com/btcsuite/btcutil/base58"
	"github.com/google/uuid"
	"github.com/lithammer/shortuuid/v4"
)

type base58Encoder struct{}

func (enc base58Encoder) Encode(u uuid.UUID) string {
	return base58.Encode(u[:])
}

func (enc base58Encoder) Decode(s string) (uuid.UUID, error) {
	return uuid.FromBytes(base58.Decode(s))
}

func main() {
	enc := base58Encoder{}
	fmt.Println(shortuuid.NewWithEncoder(enc)) // 6R7VqaQHbzC1xwA5UueGe6
}

License

MIT

Comments
  • cannot find package

    cannot find package "github.com/lithammer/shortuuid/v3"

    Hello I tried the example on README:

    package main
    
    import (
        "fmt"
    
        "github.com/lithammer/shortuuid/v3"
    )
    
    func main() {
        u := shortuuid.New() // Cekw67uyMpBGZLRP2HFVbe
    }
    

    When I run:

    go get -t -v ./...
    

    I got this error:

    github.com/lithammer/shortuuid (download)
    cannot find package "github.com/lithammer/shortuuid/v3" in any of:
    	/usr/local/go/src/github.com/lithammer/shortuuid/v3 (from $GOROOT)
    	/home/marcos/go/src/github.com/lithammer/shortuuid/v3 (from $GOPATH)
    

    but the package is available in: /home/marcos/go/src/github.com/lithammer/shortuuid

    what was my mistake?

  • switch from github.com/renstrom/shortuuid to github.com/lithammer/shortuuid make my code break

    switch from github.com/renstrom/shortuuid to github.com/lithammer/shortuuid make my code break

    I don't know why and when, github.com/renstrom/shortuuid has been switched to github.com/lithammer/shortuuid, which make my code dependence failed. Why not just fork, instead of deleting old account.

  • not enough arguments in call to uuid.Must

    not enough arguments in call to uuid.Must

    After bumping this lib with dep, I get the following error:

    vendor/github.com/renstrom/shortuuid/shortuuid.go:21:40: not enough arguments in call to uuid.Must
    	have (uuid.UUID)
    	want (uuid.UUID, error)
    

    Seems to affect v2.0.1

  • Fix `Encode()` when low half of UUID is zero

    Fix `Encode()` when low half of UUID is zero

    Previously, base57.numToString() would use number.Uint64() which is undefined if number "cannot be represented in a uint64" which is the case since it starts off containing a 128 bit integer.

    This would cause incorrect encoding of any UUID whose low half bits were all zero (along with any other cases where those bits were zero after some number of divisions by b.alphabet.Length()).

    The first added test shows an edge case that previously passed, the second an edge case that previously failed.

    Switch to using big.Int.Cmp() which works with any value.

    Fixes https://github.com/lithammer/shortuuid/issues/21

  • Use github.com/google/uuid instead of github.com/satori/go.uuid

    Use github.com/google/uuid instead of github.com/satori/go.uuid

    Given the recent issues with github.com/satori/go.uuid (https://github.com/satori/go.uuid/issues/66), which are still partly unresolved, e.g. its master isn't compatible with us (see #7, #8, #9, 10, #11 and #12).

    Even though github.com/google/uuid is only at v0.1 and clearly states this:

    This package is currently in development and the API may not be stable.

    The API will become stable with v1.

    I suspect it's a better longterm investment. And speed wise they seem pretty identical:

    google/uuid

    BenchmarkUUID-4       	  200000	   7967 ns/op	    5768 B/op	   157 allocs/op
    BenchmarkEncoding-4   	  200000	   6635 ns/op	    5776 B/op	   157 allocs/op
    BenchmarkDecoding-4   	 1000000	   2126 ns/op	    1228 B/op	    38 allocs/op
    

    satori/go.uuid

    BenchmarkUUID-4       	  200000	   8188 ns/op	    5768 B/op	   157 allocs/op
    BenchmarkEncoding-4   	  200000	   6726 ns/op	    5776 B/op	   157 allocs/op
    BenchmarkDecoding-4   	 1000000	   2156 ns/op	    1228 B/op	    38 allocs/op
    
  • Quick & Dirty update to support upstream changes.

    Quick & Dirty update to support upstream changes.

    The upstream UUID package has changed the definition of NewV4 and as a result this package has broken.

    I am not in love with this proposal, but it at least takes care of the immediate problem. I think ideally the methods should be updated to reflect the new multi-value returns seen upstream, but I did not want to destabilize head without discussion.

  • fix encoded length calculation

    fix encoded length calculation

    The encoded length should always been 22. either 32 chars * (Math.log(16)/Math.log(57)) = 21.94452487346092 or 16 bytes * 8/Math.log2(57) = 21.944524873460924

  • Bump github.com/google/uuid from 1.1.2 to 1.2.0

    Bump github.com/google/uuid from 1.1.2 to 1.2.0

    Bumps github.com/google/uuid from 1.1.2 to 1.2.0.

    Release notes

    Sourced from github.com/google/uuid's releases.

    Add NewString()

    This release introduces the NewString() function which is the equivalent of uuid.New().String().

    Syntactic cleanup

    There are no code changes. A missing period was add to a godoc comment and the linter was told to not complain that the results of hash.Write() are ignored (the function cannot fail)

    Further error optimizations

    Do not allocate memory for errors (it is only one word)

    Optimize error reporting

    Optimize length of time it takes to discover an input is bad by no longer using fmt.Errorf, which is quite slow. It now uses a custom error type that formats the string when the Error method is called rather than when generating the error.

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.

    Dependabot will merge this PR once it's up-to-date and CI passes on it, as requested by @lithammer.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
  • Removing all calls to

    Removing all calls to "uuid.Must()"

    Hi, me again, go.uuid changed back but left in uuid.Must() but it is now pretty much useless so I removed all the calls to it.

    Also would you be opposed to adding a Gopkg.toml to help mitigate these sorts of issues in the future? I'm ok if not.

  • Why base57?

    Why base57?

    Seems like base57 is an unnecessary distinction from base58 which already has pretty heavy standardization within Bitcoin and wide library support across languages see:

    https://github.com/bitcoin/bitcoin/blob/master/src/base58.cpp#L19 https://github.com/tv42/base58/blob/master/base58.go#L13 https://github.com/keis/base58/blob/master/base58.py#L17 https://github.com/mattallty/base58/blob/master/src/Allty/Utils/Base58.php#L6 https://github.com/jrdnull/base58/blob/master/lib/base58.ex#L2 https://github.com/45678/Base58/blob/master/Base58.coffee#L3 https://github.com/luke-jr/libbase58/blob/master/base58.c#L143

  • Bump golangci/golangci-lint-action from 3.3.0 to 3.3.1

    Bump golangci/golangci-lint-action from 3.3.0 to 3.3.1

    Bumps golangci/golangci-lint-action from 3.3.0 to 3.3.1.

    Release notes

    Sourced from golangci/golangci-lint-action's releases.

    v3.3.1

    What's Changed

    Full Changelog: https://github.com/golangci/golangci-lint-action/compare/v3...v3.3.1

    Commits
    • 0ad9a09 build(deps-dev): bump @​typescript-eslint/parser from 5.41.0 to 5.42.0 (#599)
    • 235ea57 build(deps-dev): bump eslint from 8.26.0 to 8.27.0 (#598)
    • a6ed001 build(deps-dev): bump @​typescript-eslint/eslint-plugin from 5.41.0 to 5.42.0 ...
    • 3a7156a build(deps-dev): bump @​typescript-eslint/parser from 5.40.1 to 5.41.0 (#596)
    • 481f8ba build(deps): bump @​types/semver from 7.3.12 to 7.3.13 (#595)
    • 06edb37 build(deps-dev): bump @​typescript-eslint/eslint-plugin from 5.40.1 to 5.41.0 ...
    • c2f79a7 build(deps): bump @​actions/cache from 3.0.5 to 3.0.6 (#593)
    • d6eac69 build(deps-dev): bump @​typescript-eslint/eslint-plugin from 5.40.0 to 5.40.1 ...
    • 7268434 build(deps-dev): bump eslint from 8.25.0 to 8.26.0 (#591)
    • a926e2b build(deps-dev): bump @​typescript-eslint/parser from 5.40.0 to 5.40.1 (#590)
    • See full diff in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Generate, encode, and decode UUIDs v1 with fast or cryptographic-quality random node identifier.

A Go package for generating and manipulating UUIDs Generate, encode, and decode UUIDs v1, as defined in RFC 4122, in Go. Project Status v1.1.0 Stable:

Sep 27, 2022
A tiny and fast Go unique string generator

Nano ID A tiny and fast Go unique string generator Safe. It uses cryptographically strong random APIs and tests distribution of symbols. Compact. It u

Nov 11, 2022
High performance unique number generator powered by Go

SEQSVR High performance unique number generator powered by Go 中文 README Features Distributed: Can be scaled horizontally High performance: Allocation

Nov 16, 2022
❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).
❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

Dec 14, 2022
golang实现的分布式唯一ID生成器distributed id generator,有全局趋势递增、严防时钟漂移、高可用、高性能等特点
golang实现的分布式唯一ID生成器distributed id generator,有全局趋势递增、严防时钟漂移、高可用、高性能等特点

ekko-idgenerator是什么 顾名思义,ekko是一个分布式唯一ID生成器,参考了snowFlake思想,但是并不局限于其设计。 名称由来 英雄联盟的时间刺客ekko 特点 易用,最大限度保证系统的易用性,支持Get与MultiGet; 高并发,单机每秒100w个唯一ID生成; 高可用,理

Jan 4, 2023
Snowflake - Simple twitter's snowflake uniquely identifiable descriptors (IDs) format generator for Go

Snowflake Dead simple and fast Twitter's snowflake id generator in Go. Installation go get github.com/HotPotatoC/snowflake Usage Generating a snowflak

Oct 6, 2022
Library to integrate github.com/google/uuid with gopkg.in/vmihailenco/msgpack

Library to integrate github.com/google/uuid with gopkg.in/vmihailenco/msgpack

Apr 26, 2022
A simple uuid library based on RFC 4122

UUID generator A simple library that generates uuids. Supported versions: version 1 version 3 version 4 version 5 Supported variants: DCE Microsoft Th

Oct 20, 2021
Compact, sortable and fast unique IDs with embedded metadata.
Compact, sortable and fast unique IDs with embedded metadata.

A spec for unique IDs in distributed systems based on the Snowflake design, i.e. a coordination-based ID variant. It aims to be friendly to both machi

Dec 22, 2022
Roche is a Code Generator and Web Framework, makes web development super concise with Go, CleanArch
Roche is a Code Generator and Web Framework, makes web development super concise with Go, CleanArch

It is still under development, so please do not use it. We plan to release v.1.0.0 in the summer. roche is a web framework optimized for microservice

Sep 19, 2022
Go concurrent-safe, goroutine-safe, thread-safe queue
Go concurrent-safe, goroutine-safe, thread-safe queue

goconcurrentqueue - Concurrent safe queues The package goconcurrentqueue offers a public interface Queue with methods for a queue. It comes with multi

Dec 31, 2022
Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.

uuid The uuid package generates and inspects UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services. This package is based on the g

Jan 1, 2023
Generate, encode, and decode UUIDs v1 with fast or cryptographic-quality random node identifier.

A Go package for generating and manipulating UUIDs Generate, encode, and decode UUIDs v1, as defined in RFC 4122, in Go. Project Status v1.1.0 Stable:

Sep 27, 2022
lru: the most concise and efficient LRU algorithm based on golang

lru This package of lru is the most concise and efficient LRU algorithm based on golang. Example Quick start: package main import ( "fmt" "github.

Dec 27, 2021
The most concise and efficient algorithm of consistent hash based on golang

consistent This package of consistent is the most concise and efficient algorithm of consistent hash based on golang. Example Quick start: package mai

Dec 28, 2021
:steam_locomotive: Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. Dual Array and Full map support.

Package form Package form Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. It has the following features: Supports map of

Dec 26, 2022
Supports the safe and convenient execution of asynchronous computations with goroutines and provides facilities for the safe retrieval of the computation results.

Rendezvous The Rendezvous library supports the safe and convenient execution of asynchronous computations with goroutines and provides facilities for

Dec 29, 2021
High-Performance Shortlink ( Short URL ) app creator in Golang. For privacy reasons, you may prefer to host your own short URL app and this is the one to use.
High-Performance Shortlink ( Short URL ) app creator in Golang. For privacy reasons, you may prefer to host your own short URL app and this is the one to use.

About The Project Shortlink App in Golang Multiple Node based Architecture to create and scale at ease Highly performant key-value storage system Cent

Jan 3, 2023
Go-based search engine URL collector , support Google, Bing, can be based on Google syntax batch collection URL
Go-based search engine URL collector , support Google, Bing, can be based on Google syntax batch collection URL

Go-based search engine URL collector , support Google, Bing, can be based on Google syntax batch collection URL

Nov 9, 2022