github.com/yitter/idgenerator-go@v1.3.3/idgen/YitIdHelper.go (about) 1 /* 2 * 版权属于:yitter(yitter@126.com) 3 * 代码编辑:guoyahao 4 * 代码修订:yitter 5 * 开源地址:https://github.com/yitter/idgenerator 6 */ 7 8 package idgen 9 10 import ( 11 "sync" 12 "time" 13 ) 14 15 var singletonMutex sync.Mutex 16 var idGenerator *DefaultIdGenerator 17 18 // SetIdGenerator . 19 func SetIdGenerator(options *IdGeneratorOptions) { 20 singletonMutex.Lock() 21 idGenerator = NewDefaultIdGenerator(options) 22 singletonMutex.Unlock() 23 } 24 25 // NextId . 26 func NextId() int64 { 27 // if idGenerator == nil { 28 // singletonMutex.Lock() 29 // defer singletonMutex.Unlock() 30 // if idGenerator == nil { 31 // options := NewIdGeneratorOptions(1) 32 // idGenerator = NewDefaultIdGenerator(options) 33 // } 34 // } 35 36 return idGenerator.NewLong() 37 } 38 39 func ExtractTime(id int64) time.Time { 40 return idGenerator.ExtractTime(id) 41 }