You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

YitIdHelper.go 878 B

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 代码编辑:guoyahao
  4. * 代码修订:yitter
  5. * 开源地址:https://gitee.com/yitter/idgenerator
  6. */
  7. package gen
  8. import (
  9. "sync"
  10. "yitidgen/contract"
  11. )
  12. var ins *YitIdHelper
  13. var once sync.Once
  14. type YitIdHelper struct {
  15. idGenInstance interface {
  16. NewLong() uint64
  17. }
  18. }
  19. func GetIns() *YitIdHelper {
  20. once.Do(func() {
  21. ins = &YitIdHelper{}
  22. })
  23. return ins
  24. }
  25. func (yih *YitIdHelper) GetIdGenInstance() interface{} {
  26. return yih.idGenInstance
  27. }
  28. func (yih *YitIdHelper) SetIdGenerator(options *contract.IdGeneratorOptions) {
  29. yih.idGenInstance = NewDefaultIdGenerator(options)
  30. }
  31. func (yih *YitIdHelper) NextId() uint64 {
  32. once.Do(func() {
  33. if yih.idGenInstance == nil {
  34. options := contract.NewIdGeneratorOptions(1)
  35. yih.idGenInstance = NewDefaultIdGenerator(options)
  36. }
  37. })
  38. return yih.idGenInstance.NewLong()
  39. }

雪花算法中非常好用的数字ID生成器