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

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

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