项目作者: lithammer

项目描述 :
:mushroom: A generator library for concise, unambiguous and URL-safe UUIDs
高级语言: Go
项目地址: git://github.com/lithammer/shortuuid.git
创建时间: 2015-10-18T19:07:46Z
项目社区:https://github.com/lithammer/shortuuid

开源协议:MIT License

下载


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

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/lithammer/shortuuid/v4"
  5. )
  6. func main() {
  7. u := shortuuid.New()
  8. fmt.Println(u) // KwSysDpxcBU9FNhGkn2dCf
  9. }

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

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

It’s possible to use a custom alphabet as well (at least 2
characters long).
It will automatically sort and remove duplicates from your alphabet to ensure consistency

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/btcsuite/btcutil/base58"
  5. "github.com/google/uuid"
  6. "github.com/lithammer/shortuuid/v4"
  7. )
  8. type base58Encoder struct{}
  9. func (enc base58Encoder) Encode(u uuid.UUID) string {
  10. return base58.Encode(u[:])
  11. }
  12. func (enc base58Encoder) Decode(s string) (uuid.UUID, error) {
  13. return uuid.FromBytes(base58.Decode(s))
  14. }
  15. func main() {
  16. enc := base58Encoder{}
  17. fmt.Println(shortuuid.NewWithEncoder(enc)) // 6R7VqaQHbzC1xwA5UueGe6
  18. }

License

MIT