rano/util/uid/sqid.go

30 lines
643 B
Go
Raw Normal View History

2024-11-17 16:58:29 +00:00
package uid
import "github.com/sqids/sqids-go"
// use your own random version of alphabets
// there is online util at the bottomt of this page: https://sqids.org/go
var opts sqids.Options = sqids.Options{
Alphabet: "fsvjrnGWiTk2Lt1l5MzEhFH73CIg60ByexQPYpX9Ro8ZKawAVUdNuDJbmqScO4",
}
// Encode a slice of IDs into one unique ID
func Encode(ids []uint64) (string, error) {
s, err := sqids.New()
if err != nil {
return "", err
}
return s.Encode(ids) // "86Rf07"
}
// Decode an ID back to slice of IDs
func Decode(id string) ([]uint64, error) {
s, err := sqids.New()
if err != nil {
return nil, err
}
return s.Decode(id), nil
}