main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package main
  2. import (
  3. "fmt"
  4. "git.aionnect.com/aionnect/go-common/utils"
  5. "strconv"
  6. )
  7. func main() {
  8. newId, _ := utils.SingletonSnowflakeKeyGen().NextId()
  9. fmt.Printf("%v\n", utils.Decompose(newId))
  10. println(len("oXnePxBaI4qUWq-X0HIU-ce6vgak"))
  11. //numCPU := runtime.NumCPU()
  12. //runtime.GOMAXPROCS(numCPU)
  13. //fmt.Println("number of cpu:", numCPU)
  14. //
  15. //consumer := make(chan utils.Long)
  16. //
  17. //const numID = 10000
  18. //generate := func() {
  19. // for i := 0; i < numID; i++ {
  20. // consumer <- utils.NextId()
  21. // }
  22. //}
  23. //
  24. //const numGenerator = 10
  25. //for i := 0; i < numGenerator; i++ {
  26. // go generate()
  27. //}
  28. //
  29. //set := mapset.NewSet()
  30. //for i := 0; i < numID*numGenerator; i++ {
  31. // id := <-consumer
  32. // if set.Contains(id) {
  33. // println("duplicated id")
  34. // return
  35. // } else {
  36. // set.Add(id)
  37. // }
  38. //}
  39. //fmt.Println("number of id:", set.Cardinality())
  40. // 临时测试代码
  41. //c, err := jws.NewJWT("hard to guess string").
  42. // ParseToken("eyJhbGciOiJIUzI1NiIsImlhdCI6MTUzNzg0Njc4MywiZXhwIjoxNTM5MDU2MzgzfQ.eyJpZCI6MzI0NTEzNH0.RqRoad8H5oAGY6L3qMLcxCUYE5Fl7-MXtpSyfSU4aqU")
  43. //if nil != err {
  44. // println(err)
  45. //}
  46. //if nil != c {
  47. // fmt.Printf("%v\n", c)
  48. // fmt.Printf(strconv.FormatFloat(c.ID, 'f', 0, 64))
  49. //}
  50. //n := 3
  51. //for i := 0; i < 500; i++ {
  52. // newId, _ := utils.SingletonSnowflakeKeyGen().NextID()
  53. //
  54. // idStr := strconv.FormatUint(newId, 10)
  55. // sLen := len(idStr)
  56. // subLen:= sLen / n
  57. // if sLen % n != 0 {
  58. // subLen++
  59. // }
  60. //
  61. // var partArr []string
  62. // var codeArr []string
  63. // for j := 0; j < n; j++ {
  64. // start := subLen * j
  65. // end := subLen * (j + 1)
  66. // if end >= sLen {
  67. // end = sLen
  68. // }
  69. // part := idStr[start:end]
  70. // partArr = append(partArr, part)
  71. //
  72. // partNum, _ := strconv.ParseInt(part, 10, 32)
  73. // code := decimalToAny(int(partNum), 35)
  74. // codeArr = append(codeArr, code)
  75. // }
  76. //
  77. // println(newId, " : ", strings.Join(partArr, "-"), " : ", strings.Join(codeArr, "-"))
  78. //}
  79. }
  80. var tenToAny = map[int]string{0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "a", 11: "b", 12: "c", 13: "d", 14: "e", 15: "f", 16: "g", 17: "h", 18: "i", 19: "j", 20: "k", 21: "l", 22: "m", 23: "n", 24: "o", 25: "p", 26: "q", 27: "r", 28: "s", 29: "t", 30: "u", 31: "v", 32: "w", 33: "x", 34: "y", 35: "z", 36: ":", 37: ";", 38: "<", 39: "=", 40: ">", 41: "?", 42: "@", 43: "[", 44: "]", 45: "^", 46: "_", 47: "{", 48: "|", 49: "}", 50: "A", 51: "B", 52: "C", 53: "D", 54: "E", 55: "F", 56: "G", 57: "H", 58: "I", 59: "J", 60: "K", 61: "L", 62: "M", 63: "N", 64: "O", 65: "P", 66: "Q", 67: "R", 68: "S", 69: "T", 70: "U", 71: "V", 72: "W", 73: "X", 74: "Y", 75: "Z"}
  81. func decimalToAny(num int, n int) string {
  82. new_num_str := ""
  83. var remainder int
  84. var remainder_string string
  85. for num != 0 {
  86. remainder = num % n
  87. if 76 > remainder && remainder > 9 {
  88. remainder_string = tenToAny[remainder]
  89. } else {
  90. remainder_string = strconv.Itoa(remainder)
  91. }
  92. new_num_str = remainder_string + new_num_str
  93. num = num / n
  94. }
  95. return new_num_str
  96. }