Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

axiaoxin/hashids

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

https://github.com/speps/go-hashids 的方法封装,数字类型的 ID 转换为随机字符串 ID

不再维护该仓库,代码已迁移到 goutils

安装:

go get -u github.com/axiaoxin/hashids

用法示例example.go

package main

import (
    "log"

    "github.com/axiaoxin/hashids"
)

func main() {
    salt := "my-salt:appid:region:uin"
    minLen := 8
    prefix := ""
    h, err := hashids.New(salt, minLen, prefix)
    if err != nil {
        log.Fatal(err)
    }
    var id int64 = 1
    strID, err := h.Encode(id)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("int64 id %d encode to %s", id, strID)

    int64ID, err := h.Decode(strID)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("string id %s decode to %d", strID, int64ID)
}

运行结果:

go run example.go
2020/02/26 13:28:11 int64 id 1 encode to 8Gnejq6A
2020/02/26 13:28:11 string id 8Gnejq6A decode to 1

About

mod: 数字类型的ID转换为随机字符串ID,https://github.com/speps/go-hashids 的方法封装

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages