github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/snow/default.go (about) 1 package snow 2 3 import "time" 4 5 // DefaultNode is the global default snowflake node object. 6 // nolint gochecknoglobals 7 var DefaultNode, _ = NewNode() 8 9 // GetOption return the option. 10 func GetOption() Option { return DefaultNode.option } 11 12 // GetEpoch returns an int64 epoch is snowflake epoch in milliseconds. 13 func GetEpoch() int64 { return DefaultNode.epoch } 14 15 // GetTime returns an int64 unix timestamp in milliseconds of the snowflake ID time. 16 func GetTime() int64 { return DefaultNode.time } 17 18 // GetNodeID returns an int64 of the snowflake ID node number 19 func GetNodeID() int64 { return DefaultNode.nodeID } 20 21 // GetStep returns an int64 of the snowflake step (or sequence) number 22 func GetStep() int64 { return DefaultNode.step } 23 24 // Next creates and returns a unique snowflake ID 25 // To help guarantee uniqueness 26 // - Make sure your system is keeping accurate system time 27 // - Make sure you never have multiple nodes running with the same node ID 28 func Next() ID { return DefaultNode.Next() } 29 30 var DefaultNode32, _ = NewNode( 31 WithNodeBits(2), 32 WithStepBits(1), 33 WithTimestampUnit(1*time.Second), 34 WithEpoch(1577808000000), // 2020-01-01T00:00:00+08:00 35 ) 36 37 // Next32 creates and returns a unique snowflake ID for positive int32. 38 // only for low frequency usages. 39 // unsigned(1) + timestamp(28) + node ID(2) + step(1) 40 // can use 2^28/60/60/24/365 ≈ 8.5 年 41 // result example: 459260906 42 // The range of int32 is [-2147483648, 2147483647] and the uint32 is [0, 4294967295]. 43 func Next32() ID { return DefaultNode32.Next() }