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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package main
  2. import (
  3. "C"
  4. "fmt"
  5. "time"
  6. "github.com/yitter/idgenerator-go/regworkerid"
  7. )
  8. func main() {
  9. // ip := "localhost"
  10. ipChar := C.CString("192.168.20.41")
  11. passChar := C.CString("")
  12. workerIdList := RegisterMany(ipChar, 6379, passChar, 4, 3, 0)
  13. for _, value := range workerIdList {
  14. fmt.Println("注册的WorkerId:", value)
  15. }
  16. id := RegisterOne(ipChar, 6379, passChar, 4, 0)
  17. fmt.Println("注册的WorkerId:", id)
  18. // C.free(unsafe.Pointer(ipChar))
  19. // C.free(unsafe.Pointer(passChar))
  20. // var workerId = regworkerid.RegisterOne(ip, 6379, "", 4)
  21. // fmt.Println("注册的WorkerId:", workerId)
  22. fmt.Println("end")
  23. time.Sleep(time.Duration(300) * time.Second)
  24. }
  25. //export RegisterOne
  26. // 注册一个 WorkerId,会先注销所有本机已注册的记录
  27. func RegisterOne(ip *C.char, port int32, password *C.char, maxWorkerId int32, database int) int32 {
  28. return regworkerid.RegisterOne(C.GoString(ip), port, C.GoString(password), maxWorkerId, database)
  29. }
  30. // RegisterMany
  31. // 注册多个 WorkerId,会先注销所有本机已注册的记录
  32. func RegisterMany(ip *C.char, port int32, password *C.char, maxWorkerId, totalCount int32, database int) []int32 {
  33. // return (*C.int)(unsafe.Pointer(&values))
  34. //return regworkerid.RegisterMany(ip, port, password, maxWorkerId, totalCount, database)
  35. return regworkerid.RegisterMany(C.GoString(ip), port, C.GoString(password), maxWorkerId, totalCount, database)
  36. }
  37. //export UnRegister
  38. // 注销本机已注册的 WorkerId
  39. func UnRegister() {
  40. regworkerid.UnRegister()
  41. }
  42. //export Validate
  43. // 检查本地WorkerId是否有效(0-有效,其它-无效)
  44. func Validate(workerId int32) int32 {
  45. return regworkerid.Validate(workerId)
  46. }
  47. // To Build a dll/so:
  48. // windows:
  49. // go build -o ./target/yitidgengo.dll -buildmode=c-shared main.go
  50. // // go build -o ./target/yitidgengo.dll -buildmode=c-shared main.go reg.go
  51. // linux init: go install -buildmode=shared -linkshared std
  52. // go build -o ./target/yitidgengo.so -buildmode=c-shared main.go
  53. // go build -o ./target/yitidgengo.so -buildmode=c-shared main.go reg.go
  54. // https://books.studygolang.com/advanced-go-programming-book/ch2-cgo/ch2-09-static-shared-lib.html