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

4 years ago
3 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
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package main
  2. import (
  3. "C"
  4. "fmt"
  5. "time"
  6. "github.com/yitter/idgenerator-go/idgen"
  7. )
  8. func SetOptions(workerId uint16) {
  9. var options = idgen.NewIdGeneratorOptions(workerId)
  10. idgen.SetIdGenerator(options)
  11. }
  12. func NextId() int64 {
  13. return idgen.NextId()
  14. }
  15. func main() {
  16. const testGenId = true // 测试设置
  17. if testGenId {
  18. // 自定义参数
  19. var options = idgen.NewIdGeneratorOptions(1)
  20. options.WorkerIdBitLength = 6
  21. options.SeqBitLength = 10
  22. options.BaseTime = time.Date(2020, 2, 20, 2, 20, 2, 20, time.UTC).UnixNano() / 1e6
  23. idgen.SetIdGenerator(options)
  24. var genCount = 500000
  25. for j := 0; j < 100000; j++ {
  26. for {
  27. var begin = time.Now().UnixNano() / 1e6
  28. for i := 0; i < genCount; i++ {
  29. // 生成ID
  30. idgen.NextId()
  31. // fmt.Println(id)
  32. }
  33. var end = time.Now().UnixNano() / 1e6
  34. fmt.Println("耗时:", (end - begin), "ms")
  35. time.Sleep(time.Duration(1000) * time.Millisecond)
  36. }
  37. }
  38. } else {
  39. // ip := "localhost"
  40. ipChar := C.CString("localhost")
  41. passChar := C.CString("")
  42. workerIdList := RegisterMany(ipChar, 6379, passChar, 4, 3, 0)
  43. for _, value := range workerIdList {
  44. fmt.Println("注册的WorkerId:", value)
  45. }
  46. id := RegisterOne(ipChar, 6379, passChar, 4, 0)
  47. fmt.Println("注册的WorkerId:", id)
  48. // C.free(unsafe.Pointer(ipChar))
  49. // C.free(unsafe.Pointer(passChar))
  50. // var workerId = regworkerid.RegisterOne(ip, 6379, "", 4)
  51. // fmt.Println("注册的WorkerId:", workerId)
  52. fmt.Println("end")
  53. time.Sleep(time.Duration(300) * time.Second)
  54. }
  55. }
  56. // To Build a dll/so:
  57. // windows:
  58. // go build -o ./target/yitidgengo.dll -buildmode=c-shared main.go reg.go
  59. // linux init: go install -buildmode=shared -linkshared std
  60. // go build -o ./target/yitidgengo.so -buildmode=c-shared main.go reg.go
  61. // https://books.studygolang.com/advanced-go-programming-book/ch2-cgo/ch2-09-static-shared-lib.html