Coder Social home page Coder Social logo

token's Introduction

Use v2 Instead

Warning: Breaking Changes

Version 2 of this package is ready for production use. You can find it here.

The order of the Base62 characters have been changed in v2 so that the string representation of the Token and the int representation of the token are in the same sort order. This is useful when scaling your app or using NoSQL solutions. Special thanks to @sudhirj for the suggestion.

References

https://instagram-engineering.com/sharding-ids-at-instagram-1cf5a71e5a5c

https://developer.twitter.com/en/docs/basics/twitter-ids.html

https://github.com/ulid/spec


GoDoc

This is a simple package for go 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.

How it Works

Token is an alias for uint64.

The Token.Encode() method returns a base62 encoded string based off of 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

Example

package main

import (
	"fmt"
	"github.com/marksalpeter/token"
)

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

}

Special Mentions

Special thanks to @einsteinx2. The encode and decode functions are ported from a short url project of his and he graciously allowed me to publish them.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.