标签:自增 bit 算法 ffffff 标识 导致 单机 标示 div
go get github.com/sony/sonyflake
Demo
package main import ( "fmt" "github.com/sony/sonyflake" ) var ( LSonyFlake *sonyflake.Sonyflake machineId uint16 // 真正的分布式环境下必须zookeeper或etcd中获取 ) func getMachineID() (uint16, error) { return machineId, nil } func Init(mid uint16) (err error) { machineId = mid st := sonyflake.Settings{} st.MachineID = getMachineID LSonyFlake = sonyflake.NewSonyflake(st) return } func GetID()(id uint64,err error) { if LSonyFlake == nil{ err = fmt.Errorf("No Init\n") return } return LSonyFlake.NextID() } func main() { Init(0) id,err:=GetID() if err != nil { fmt.Println(err) } fmt.Println(id) }
标签:自增 bit 算法 ffffff 标识 导致 单机 标示 div
原文地址:https://www.cnblogs.com/binHome/p/12072433.html