Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IdGenerator ¶
type IdGenerator struct {
// contains filtered or unexported fields
}
IdGenerator 雪花算法 id 生成器
原理: https://en.wikipedia.org/wiki/Snowflake_ID 原作者: 小生凡一
参考文章: 美团Leaf方案 https://tech.meituan.com/2017/04/21/mt-leaf.html 百度UidGenerator方案 https://zhuanlan.zhihu.com/p/550596015
func New ¶
func New(dataCenterId, workerId uint64, start ...time.Time) *IdGenerator
创建ID生成器实例
- {dataCenterId} 集群ID [0, 31]
- {workerId} 程序ID [0, 31]
- {start} 可选, 设置起始点, 未来时间会置为默认 (默认为 UTC: 2024-01-01 00:00:00)
func (*IdGenerator) GenLong ¶ added in v0.6.1
func (ig *IdGenerator) GenLong(full bool) string
GenLong 生成长唯一ID
- {full} 是否全量, 全量为 128 位固长字串, 否则为 64 位长度
注: 不能直接用于密钥生成. 雪花算法强调ID有序,而密钥强调随机. 即使用也需要加上 math/rand 随机字串
func (*IdGenerator) GenNum ¶
func (ig *IdGenerator) GenNum() uint64
GenNum 生成雪花算法ID (10进制位数值)
注: 经测试、返回 uint64 可无损用 int64 表示(因 snowflake 内部实现上仅用了 63 位二进制位)
Example 更简短表达:
fmt.Sprintf("%X", GenNum()) //16进制位 (0-9 + a-f)
fmt.Sprintf("%016x", GenNum()) //固定16位字串
strconv.FormatUint(GenNum(), 36) //36进制位 (0-9 + a-z)
Click to show internal directories.
Click to hide internal directories.