This is a simple package that generates randomized base62 encoded tokens based on an integer. It's ideal for short url services or for any short, unique, randomized tokens you need to use throughout your app.
This repo is a fork of marksalpeter/token.
Special thanks to @einsteinx2. The encode and decode functions were ported from a short url project of his and he graciously allowed @marksalpeter to publish them.
Special thanks to @sudhirj for encorperating lexical sort order of the tokens into the package.
I originally forked it before the parent project adopted modules, so that I can add them. Since then, I've cleaned up the code a tiny bit, got linters to pass and eliminated all dependencies and reorganized the README a bit.
Token is an alias for uint64.
The Token.Encode() method returns a base62 encoded string based off of the uint64.
The string will always be in the same sort order as the uint64.
Token implements the encoding.TextMarshaler and encoding.TextUnmarshaler interfaces
to encode and decode to and from the base62 string representation of the uint64
Basically, the outside world will always see the token as a base62 encoded string,
but in your app you will always be able to use the token as a uint64 for fast,
indexed, unique, lookups in various databases.
IMPORTANT: Remember to always check for collisions when adding randomized tokens to a database
package main
import (
"fmt"
"github.com/alexaandru/token/v2"
)
type Model struct {
ID token.Token `json:"id"`
}
func main() {
// create a new model
model := Model {
ID: token.New(), // creates a new, random uint64 token
}
fmt.Println(model.ID) // 2751173559858
fmt.Println(model.ID.Encode()) // Mr1NSSu
// encode the model as json
marshaled, err := json.Marshal(&model)
if err != nil {
panic(err)
}
fmt.Println(string(marshaled)) // {"id":"Mr1NSSu"}
// decode the model
var unmarshaled Model
if err := json.Unmarshal(marshaled, &unmarshaled); err != nil {
panic(err)
}
fmt.Println(unmarshaled.ID) // 2751173559858
}