代码拉取完成,页面将自动刷新
package custom
import (
"encoding/binary"
"encoding/hex"
"sync/atomic"
)
type (
CustomUUID struct {
cfg *Options
mid uint32
count uint32
}
)
func (c *CustomUUID) New() [16]byte {
var res [16]byte
var mid uint16
num := atomic.AddUint32(&c.count, 1)
if num == 0 {
mid = uint16(atomic.AddUint32(&c.mid, 1) & 0x0000ffff)
} else {
mid = uint16(atomic.LoadUint32(&c.mid) & 0x0000ffff)
}
if mid == 0 && num == 0 {
panic("INVALID ID GEN ")
}
binary.BigEndian.PutUint16(res[0:4], c.cfg.Seed)
binary.BigEndian.PutUint16(res[4:], mid)
binary.BigEndian.PutUint32(res[8:], num)
return res
}
func (c *CustomUUID) New32UUID() uint32 {
num := atomic.AddUint32(&c.count, 1) & 0x0000ffff
if num == 0 {
panic("INVALID ID GEN")
}
return uint32(c.cfg.Seed)<<16 + num
}
func (c *CustomUUID) New64UUID() uint64 {
var mid uint16
num := atomic.AddUint32(&c.count, 1)
if num == 0 {
mid = uint16(atomic.AddUint32(&c.mid, 1) & 0x0000ffff)
} else {
mid = uint16(atomic.LoadUint32(&c.mid) & 0x0000ffff)
}
return uint64(c.cfg.Seed)<<48 + uint64(mid)<<32 + uint64(num)
}
// NewStringUUID will return rfc 2141 uuid
func (c *CustomUUID) NewStringUUID() string {
var dist [36 + 9]byte
res := c.New()
hex.Encode(dist[:], res[0:4])
dist[8] = '-'
hex.Encode(dist[9:13], res[4:6])
dist[13] = '-'
hex.Encode(dist[14:18], res[6:8])
dist[18] = '-'
hex.Encode(dist[19:23], res[8:10])
dist[23] = '-'
hex.Encode(dist[24:], res[10:])
return string(dist[:])
}
func (c *CustomUUID) ParseFromByte(bytes []byte) string {
panic("implement me")
}
func NewCustomUUID(opts ...Option) *CustomUUID {
idGen := &CustomUUID{
cfg: &Options{},
mid: 1,
count: 1,
}
for _, opt := range opts {
opt(idGen.cfg)
}
return idGen
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。