shortid

package module
v0.0.0-...-6ed30cc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2018 License: MIT Imports: 9 Imported by: 12

README

ShortId

Sometimes, we couldn't use GUID/UUID as primary key in our application. In this case, we have to generate a short id to resovle the problem. The package generates a short id which is 12 characters. For instance, 15DE398FEB83. The first two characters represent to current year, and characters which between 3 to 10 are random. The last two characters are machineId. I run 1M times and there is no duplicate key.

Usuage

package main

import shortid "github.com/jasonsoft/go-short-id"

func main() {
	opt := Options{
		Number:        14,
		StartWithYear: true,
		EndWithHost:   false,
	}
	id := shortid.Generate(opt)
	println(id) // output: 17GuOMTTeMKZQpxZ
}

Reference:

.Net Version

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(opt Options) string

func GetCounter

func GetCounter(prefixStr string) (int64, error)

GetCounter get the date serial number return int 64 (Example: 170817000000001) It will return an error if the redis error

func SetRedis

func SetRedis(opt RedisConfig)

SetRedis setting the counter redis

Types

type Options

type Options struct {
	Number        int
	StartWithYear bool
	EndWithHost   bool
}

type RedisConfig

type RedisConfig redis.Options

RedisConfig the configuration of redis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL