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.

main.go 758 B

4 years ago
123456789101112131415161718192021222324252627282930313233343536
  1. package main
  2. import (
  3. "../yitidgen/contract"
  4. "../yitidgen/idgen"
  5. "fmt"
  6. "time"
  7. )
  8. func main() {
  9. // 方法一:直接采用默认方法生成一个Id
  10. var yid = idgen.YitIdHelper{}
  11. fmt.Println(yid.NextId())
  12. // 方法二:自定义参数
  13. var options = contract.NewIdGeneratorOptions(1)
  14. //options.WorkerIdBitLength = 6
  15. //options.SeqBitLength = 6
  16. //options.TopOverCostCount = 2000
  17. //options.BaseTime = time.Date(2020, 2, 20, 2, 20, 2, 20, time.UTC).UnixNano() / 1e6
  18. yid.SetIdGenerator(options)
  19. var times = 50000
  20. for ; ; {
  21. var begin = time.Now().UnixNano() / 1e6
  22. for i := 0; i < times; i++ {
  23. yid.NextId()
  24. }
  25. var end = time.Now().UnixNano() / 1e6
  26. fmt.Println(end - begin)
  27. time.Sleep(time.Duration(1000) * time.Millisecond)
  28. }
  29. }

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