shortuuid

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

Github星跟踪图

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/v3"
)

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

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) // u=BFWRLr5dXbeWf==iasZi

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

package main

import (
    "fmt"
    "github.com/btcsuite/btcutil/base58"
    "github.com/lithammer/shortuuid/v3"
    "github.com/satori/go.uuid"
)

type base58Encoder struct {}

func (enc base58Encoder) Encode(u uuid.UUID) string {
    return base58.Encode(u.Bytes())
}

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

主要指标

概览
名称与所有者lithammer/shortuuid
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2015-10-18 19:07:46
推送于2025-06-01 21:05:24
最后一次提交2025-06-01 23:04:24
发布数15
最新版本名称v4.2.0 (发布于 )
第一版名称v1.0.0 (发布于 2015-11-24 13:05:30)
用户参与
星数1.3k
关注者数10
派生数61
提交数206
已启用问题?
问题数14
打开的问题数1
拉请求数47
打开的拉请求数3
关闭的拉请求数7
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?