用命令获取第三方包
go get -u -v github.com/speps/go-hashids
代码如下:
package hashids
import (
"github.com/speps/go-hashids"
)
//salt 盐值
const salt = "salt"
//Encode 混淆
func Encode(data int) string {
hd := hashids.NewData()
hd.Salt = salt
h, _ := hashids.NewWithData(hd)
e, _ := h.Encode([]int{data})
return e
}
//Decode 还原混淆
func Decode(data string) int {
hd := hashids.NewData()
hd.Salt = salt
h, _ := hashids.NewWithData(hd)
e, _ := h.DecodeWithError(data)
return e[0]
}
有疑问加站长微信联系(非本文作者)