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

4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/yitter/idgenerator-go/idgen"
  6. )
  7. func SetOptions(workerId uint16) {
  8. var options = idgen.NewIdGeneratorOptions(workerId)
  9. idgen.SetIdGenerator(options)
  10. }
  11. func NextId() int64 {
  12. return idgen.NextId()
  13. }
  14. func main() {
  15. // 自定义参数
  16. var options = idgen.NewIdGeneratorOptions(1)
  17. options.WorkerIdBitLength = 6
  18. options.SeqBitLength = 10
  19. options.BaseTime = time.Date(2020, 2, 20, 2, 20, 2, 20, time.UTC).UnixNano() / 1e6
  20. idgen.SetIdGenerator(options)
  21. var genCount = 500000
  22. for j := 0; j < 100000; j++ {
  23. for {
  24. var begin = time.Now().UnixNano() / 1e6
  25. for i := 0; i < genCount; i++ {
  26. // 生成ID
  27. idgen.NextId()
  28. // fmt.Println(id)
  29. }
  30. var end = time.Now().UnixNano() / 1e6
  31. fmt.Println("耗时:", (end - begin), "ms")
  32. time.Sleep(time.Duration(1000) * time.Millisecond)
  33. }
  34. }
  35. }
  36. // To Build a dll/so:
  37. // windows:
  38. // go build -o ./target/yitidgengo.dll -buildmode=c-shared main.go
  39. // linux init: go install -buildmode=shared -linkshared std
  40. // go build -o ./target/yitidgengo.so -buildmode=c-shared main.go
  41. // https://books.studygolang.com/advanced-go-programming-book/ch2-cgo/ch2-09-static-shared-lib.html