天天看點

Golang:go-hashids從整數生成短唯一ID

generate short unique ids from integers

譯文:從整數生成短唯一ID

文檔

  • 官網:​​https://hashids.org/go/​​
  • github:​​https://github.com/speps/go-hashids​​
  • pkg:​​https://pkg.go.dev/github.com/speps/go-hashids/v2​​

安裝

go get github.com/speps/go-hashids/v2      
package main

import (
    "fmt"

    "github.com/speps/go-hashids/v2"
)

func main() {
    hashID, _ := hashids.New()

    id, _ := hashID.Encode([]int{1, 2, 3})
    fmt.Printf("id: %v\n", id)
    // id: o2fXhV

    numbers := hashID.Decode(id)
    fmt.Printf("numbers: %v\n", numbers)
    // numbers: [1 2 3]
}      

繼續閱讀