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

4 years ago
2 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
2 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 = 6
  22. options.BaseTime = time.Date(2020, 2, 20, 2, 20, 2, 20, time.UTC).UnixNano() / 1e6
  23. idgen.SetIdGenerator(options)
  24. var genCount = 50000
  25. for {
  26. var begin = time.Now().UnixNano() / 1e3
  27. for i := 0; i < genCount; i++ {
  28. // 生成ID
  29. id := idgen.NextId()
  30. fmt.Println(id)
  31. }
  32. var end = time.Now().UnixNano() / 1e3
  33. fmt.Println(end - begin)
  34. time.Sleep(time.Duration(1000) * time.Millisecond)
  35. }
  36. } else {
  37. // ip := "localhost"
  38. ipChar := C.CString("localhost")
  39. passChar := C.CString("")
  40. workerIdList := RegisterMany(ipChar, 6379, passChar, 4, 3, 0)
  41. for _, value := range workerIdList {
  42. fmt.Println("注册的WorkerId:", value)
  43. }
  44. id := RegisterOne(ipChar, 6379, passChar, 4, 0)
  45. fmt.Println("注册的WorkerId:", id)
  46. // C.free(unsafe.Pointer(ipChar))
  47. // C.free(unsafe.Pointer(passChar))
  48. // var workerId = regworkerid.RegisterOne(ip, 6379, "", 4)
  49. // fmt.Println("注册的WorkerId:", workerId)
  50. fmt.Println("end")
  51. time.Sleep(time.Duration(300) * time.Second)
  52. }
  53. }
  54. // To Build a dll/so:
  55. // windows:
  56. // go build -o ./target/yitidgengo.dll -buildmode=c-shared main.go reg.go
  57. // linux init: go install -buildmode=shared -linkshared std
  58. // go build -o ./target/yitidgengo.so -buildmode=c-shared main.go reg.go
  59. // https://books.studygolang.com/advanced-go-programming-book/ch2-cgo/ch2-09-static-shared-lib.html