gid

package
v0.0.0-...-7429660 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

README

分布式id生成器

特点
  • 支持高并发
  • 附带时间戳,相对有序
  • 不依赖中心节点
使用
package main

import (
	"git.inke.cn/BackendPlatform/gid"
	"fmt"
)

func main() {
	id := gid.New()
	fmt.Printf("16进制:%x\n", id)
	fmt.Printf("10进制:%d\n", id)

	s := fmt.Sprintf("%x", id)
	want := gid.UnixFromUint64(id)
	got := gid.UnixFromStr(s)
	if got != want {
		fmt.Printf("parse err,got %d,want %d", got, want)
	}
	fmt.Printf("解析时间戳:got %d,want %d\n", got, want)
}
/*
16进制:7a16ed3f66fb5
10进制:2147822211592117
解析时间戳:got 1554273261,want 1554273261
*/

生成方式

规则:28bit(duration)+16bit(iphash)+20bit(random num)

参考文档

分布式id生成器

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fnv32a

func Fnv32a(s []byte) uint32

func Fnv32aExt

func Fnv32aExt(s []byte) uint64

func FnvCodeFromStr

func FnvCodeFromStr(s string) uint32

func FnvCodeFromUint64

func FnvCodeFromUint64(v uint64) uint32

func HashCode

func HashCode(s []byte) uint32

func HostIP

func HostIP() (net.IP, error)

HostIP tries to find an IP that can be used by other machines to reach this machine.

func HostnameHashCode

func HostnameHashCode() uint32

func IpHashCode

func IpHashCode() uint32

func New

func New() uint64

func NewGId

func NewGId() *gid

func NewRand

func NewRand(seed int64) *rand.Rand

func RandomFromStr

func RandomFromStr(s string) uint32

func RandomFromUint64

func RandomFromUint64(v uint64) uint32

func SplitId

func SplitId(s string) (low string, high string)

func StrToUint64

func StrToUint64(s string) uint64

func UUID

func UUID() string

UUID returns a rand UUID string

func UnixFromStr

func UnixFromStr(s string) int64
Example
s := "a376611a0fc5a696bbfefc84"
low, high := SplitId(s)
fmt.Println(low, high)
fmt.Println(UnixFromStr(low))
fmt.Println(time.Unix(UnixFromStr(low), 0))
fmt.Println(FnvCodeFromStr(low))
fmt.Println(RandomFromStr(low))
Output:

func UnixFromUint64

func UnixFromUint64(v uint64) int64

Types

This section is empty.

Jump to

Keyboard shortcuts

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