Giter Site home page Giter Site logo

basex's Introduction

basex

GoDoc Build Status

A native golang implementation for basex encoding which produces youtube like video id. There are only 10 digits to work with, so if you have a lot of records to maintain in the application, IDs tend to get very lengthy. uuidgen gives a very lengthy value. We can use characters from the alphabet as have them pose as additional numbers.

Or how to create IDs similar to YouTube e.g. yzNjIBEdyww

The alphabet has 26 characters. That's a lot more than 10 digits. If we also distinguish upper- and lowercase, and add digits to the bunch for the heck of it, we already have (26 x 2 + 10) 62 options we can use per position in the ID. Please note that this package only takes numeric inputs.

Note:

11/14/2015 version 0.1.0 has a breaking change which has new 'error' return type.
06/17/2016 version 0.1.1 has new functions EncodeInt and DecodeInt for processing big integers directly.

Usage

package main

import (
        "fmt"
        "math/big"
        "github.com/dineshappavoo/basex"
)

func main() {
        input := "123456789012345678901234567890"
        inputBigInt := big.NewInt(0)
        inputBigInt.SetString(input, 10)

        fmt.Println("Input : ", input)

        // encode and decode functions
        encoded, err := basex.Encode(input)
        if err != nil {
                fmt.Println(err)
        }
        fmt.Println("Encoded : ", encoded)

        decoded, err := basex.Decode(encoded)
        if err != nil {
                fmt.Println(err)
        }
        fmt.Println("Decoded : ", decoded)

        if input == decoded {
                fmt.Println("Passed! decoded value is the same as the original.")
        } else {
                fmt.Println("FAILED! decoded value is NOT the same as the original!!")
        }

        // encode int and decode int functions
        encodedInt, err := basex.EncodeInt(inputBigInt)
        if err != nil {
                fmt.Println(err)
        }
        fmt.Println("Encoded using big int: ", encodedInt)

        decodedInt, err := basex.DecodeInt(encodedInt)
        if err != nil {
                fmt.Println(err)
        }
        fmt.Println("Decoded using big int: ", decodedInt)

        if inputBigInt.Cmp(decodedInt) == 0 {
                fmt.Println("Passed! decoded int value is the same as the intput big int.")
        } else {
                fmt.Println("FAILED! decoded int value is NOT the same as the original!!")
        }
}

output looks like,

Input :  123456789012345678901234567890

Encoded :  2aYls9bkamJJSwhr0
Decoded :  123456789012345678901234567890
Passed! decoded value is the same as the original.

Encoded using big int:  2aYls9bkamJJSwhr0
Decoded using big int:  123456789012345678901234567890
Passed! decoded int value is the same as the intput big int.

Install

go get github.com/dineshappavoo/basex

Referrence

Project Contributor(s)

basex's People

Contributors

dineshappavoo avatar daved avatar ufoot avatar guilhermebr avatar jimt avatar bryant1410 avatar

Watchers

James Cloos avatar

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.