您当前的位置: 首页 >  golang

彭世瑜

暂无认证

  • 3浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Golang:go-hashids从整数生成短唯一ID

彭世瑜 发布时间:2022-09-14 11:21:45 ,浏览量:3

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]
}

参考 「Go工具箱」一个将非负整数转换成唯一、无序ID的工具:hashids

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.2931s