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 1.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "C"
  4. "fmt"
  5. "time"
  6. "yitidgen/contract"
  7. "yitidgen/idgen"
  8. "yitidgen/regworkerid"
  9. )
  10. ///export SetOptions
  11. func SetOptions(workerId uint16) {
  12. var options = contract.NewIdGeneratorOptions(workerId)
  13. idgen.SetIdGenerator(options)
  14. }
  15. ///export NextId
  16. func NextId() uint64 {
  17. return idgen.NextId()
  18. }
  19. // 注册一个新的WorkerId
  20. //export RegisterWorkerId
  21. func RegisterWorkerId(ip *C.char, port int, password *C.char, maxWorkerId int) int {
  22. return int(regworkerid.RegisterWorkerId(C.GoString(ip), port, C.GoString(password), maxWorkerId))
  23. }
  24. // 注销WorkerId
  25. //export UnRegisterWorkerId
  26. func UnRegisterWorkerId() {
  27. regworkerid.UnRegisterWorkerId()
  28. }
  29. // 检查本地WorkerId是否有效(0-有效,其它-无效)
  30. //export ValidateLocalWorkerId
  31. func ValidateLocalWorkerId(workerId int) int {
  32. return regworkerid.ValidateLocalWorkerId(workerId)
  33. }
  34. func main() {
  35. // 方法一:直接采用默认方法生成一个Id
  36. fmt.Println("生成的Id:", idgen.NextId())
  37. fmt.Println("注册的WorkerId:", regworkerid.RegisterWorkerId("localhost", 6379, "", 4))
  38. return
  39. // 方法二:自定义参数
  40. var options = contract.NewIdGeneratorOptions(1)
  41. options.WorkerIdBitLength = 6
  42. options.SeqBitLength = 6
  43. options.TopOverCostCount = 2000
  44. options.BaseTime = time.Date(2020, 2, 20, 2, 20, 2, 20, time.UTC).UnixNano() / 1e6
  45. idgen.SetIdGenerator(options)
  46. var genCount = 50000
  47. for {
  48. var begin = time.Now().UnixNano() / 1e6
  49. for i := 0; i < genCount; i++ {
  50. idgen.NextId()
  51. }
  52. var end = time.Now().UnixNano() / 1e6
  53. fmt.Println(end - begin)
  54. time.Sleep(time.Duration(1000) * time.Millisecond)
  55. }
  56. }
  57. // go build -o target\yitidgengo.dll -buildmode=c-shared main.go
  58. //var yid = idgen.YitIdHelper{}
  59. //yid.SetIdGenerator(options)
  60. //fmt.Println(yid.NextId())

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