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 821 B

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

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