uid

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2021 License: MIT Imports: 6 Imported by: 0

README

GitHub Go Report Card GitHub issues GitHub code size in bytes made-with-Go FOSSA Status CircleCI

int-uid

int-uid

Experimental project to generate integer unique identifier.

Docs: https://pkg.go.dev/github.com/kecci/int-uid

Installation

$ go get github.com/kecci/int-uid

Usage

examples main.go:

package main

import (
	"fmt"
	"time"

	uid "github.com/kecci/int-uid"
)

func main() {
	// Unix Nano
	fmt.Println("UnixNano")
	unix := uid.New().UnixNano()
	unixReverse := unix.Reverse()
	fmt.Println(unix.Int64())
	fmt.Println(unixReverse.Int64())

	// Snowflake
	fmt.Println("Snowflake")
	snow := uid.New().Snowflake()
	snowReverse := snow.Reverse()
	fmt.Println(snow.Int64())
	fmt.Println(snowReverse.Int64())

	// Sonyflake
	fmt.Println("Sonyflake")
	sony := uid.New().Sonyflake(time.Time{})
	sonyReverse := sony.Reverse()
	fmt.Println(sony.Int64())
	fmt.Println(sonyReverse.Int64())
}

output :

UnixNano
1614436846108942000
2498016486344161
Snowflake
1365673231834419200
29144381323765631
Sonyflake
3999050837534991611
1161994357380509993

Data Types

There are several functions to get int-uid.

Func Bit Length Go Data Type SQL Data Type Example
UnixNano() 64 bit 19 Character int64 BigInt 1614432967521585000
UnixNano().Reverse() 64 bit 16 Character int64 BigInt 1261257692344161
Snowflake() 64 bit 19 Character int64 BigInt 1365656963861450752
Snowflake().Reverse() 64 bit 19 Character int64 BigInt 2570541683696565631
Sonyflake(time.Time) 64 bit 19 Character int64 BigInt 3999050837534991611
Sonyflake(time.Time).Reverse() 64 bit 19 Character int64 BigInt 1161994357380509993

Benchmark

goos: darwin
goarch: amd64
pkg: github.com/kecci/int-uid
BenchmarkUnixNano/UnixNano()-8          		1000000000      0.000001 ns/op      0 B/op      0 allocs/op
BenchmarkUnixNano/UnixNano().Reverse()-8        1000000000      0.000005 ns/op      0 B/op      0 allocs/op
BenchmarkSnowflake/Snowflake()-8                1000000000      0.000002 ns/op      0 B/op      0 allocs/op
BenchmarkSnowflake/Snowflake().Reverse()-8      1000000000      0.000002 ns/op      0 B/op      0 allocs/op
BenchmarkSonyflake/Sonyflake()-8                1000000000      0.000049 ns/op      0 B/op      0 allocs/op
BenchmarkSonyflake/Sonyflake().Reverse()-8      1000000000      0.000047 ns/op      0 B/op      0 allocs/op
PASS
coverage: 100.0% of statements
ok      github.com/kecci/int-uid        0.179s

License

FOSSA Status

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ID

type ID int64

ID custom type used for int-uid

func (ID) Int64

func (i ID) Int64() int64

Int64 convert to int64

func (ID) Reverse

func (i ID) Reverse() ID

Reverse ID

type UID

type UID struct {
	// contains filtered or unexported fields
}

UID basic info

func New

func New() *UID

New initiate UID

func (*UID) Snowflake

func (u *UID) Snowflake() ID

Snowflake generate snowflake id

func (*UID) Sonyflake added in v0.1.1

func (u *UID) Sonyflake(startTime time.Time) ID

func (*UID) UnixNano

func (u *UID) UnixNano() ID

UnixNano generate unixnano id

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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