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.5 kB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. //export RegisterWorkerId
  20. func RegisterWorkerId(ip *C.char, port int, password *C.char, maxWorkerId int) int {
  21. return regworkerid.RegisterWorkerId(C.GoString(ip), port, C.GoString(password), maxWorkerId)
  22. }
  23. //export UnRegisterWorkerId
  24. func UnRegisterWorkerId() {
  25. regworkerid.UnRegisterWorkerId()
  26. }
  27. func main() {
  28. // 方法一:直接采用默认方法生成一个Id
  29. fmt.Println("生成的Id:", idgen.NextId())
  30. fmt.Println("生成的Id:", regworkerid.RegisterWorkerId("localhost", 6379, "", 4))
  31. return
  32. // 方法二:自定义参数
  33. var options = contract.NewIdGeneratorOptions(1)
  34. options.WorkerIdBitLength = 6
  35. options.SeqBitLength = 6
  36. options.TopOverCostCount = 2000
  37. options.BaseTime = time.Date(2020, 2, 20, 2, 20, 2, 20, time.UTC).UnixNano() / 1e6
  38. idgen.SetIdGenerator(options)
  39. var genCount = 50000
  40. for {
  41. var begin = time.Now().UnixNano() / 1e6
  42. for i := 0; i < genCount; i++ {
  43. idgen.NextId()
  44. }
  45. var end = time.Now().UnixNano() / 1e6
  46. fmt.Println(end - begin)
  47. time.Sleep(time.Duration(1000) * time.Millisecond)
  48. }
  49. }
  50. // go build -o target\yitidgengo.dll -buildmode=c-shared main.go
  51. //var yid = idgen.YitIdHelper{}
  52. //yid.SetIdGenerator(options)
  53. //fmt.Println(yid.NextId())

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