lib.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package lib
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "sync"
  7. )
  8. type Cl_ struct {
  9. Uuid_list map[string]string // 泛型
  10. }
  11. var SubClinets sync.Map // 订阅Map
  12. func init() {
  13. }
  14. func To_float32(value interface{}) float32 {
  15. var key float32
  16. if value == nil {
  17. return key
  18. }
  19. switch value.(type) {
  20. case float64:
  21. key = float32(value.(float64))
  22. case float32:
  23. key = float32(value.(float32))
  24. case int:
  25. key = float32(value.(int))
  26. case uint:
  27. key = float32(value.(uint))
  28. case int8:
  29. key = float32(value.(int8))
  30. case uint8:
  31. key = float32(value.(uint8))
  32. case int16:
  33. key = float32(value.(int16))
  34. case uint16:
  35. key = float32(value.(uint16))
  36. case int32:
  37. key = float32(value.(int32))
  38. case uint32:
  39. key = float32(value.(uint32))
  40. case int64:
  41. key = float32(value.(int64))
  42. case uint64:
  43. key = float32(value.(uint64))
  44. case string:
  45. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  46. key = float32(key_float64)
  47. case []byte:
  48. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  49. key = float32(key_float64)
  50. default:
  51. newValue, _ := json.Marshal(value)
  52. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  53. key = float32(key_float64)
  54. }
  55. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  56. key = float32(key_float64)
  57. return key
  58. }
  59. func To_string(value interface{}) string {
  60. var key string
  61. if value == nil {
  62. return key
  63. }
  64. switch value.(type) {
  65. case float64:
  66. ft := value.(float64)
  67. key = strconv.FormatFloat(ft, 'f', -1, 64)
  68. case float32:
  69. ft := value.(float32)
  70. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  71. case int:
  72. it := value.(int)
  73. key = strconv.Itoa(it)
  74. case uint:
  75. it := value.(uint)
  76. key = strconv.Itoa(int(it))
  77. case int8:
  78. it := value.(int8)
  79. key = strconv.Itoa(int(it))
  80. case uint8:
  81. it := value.(uint8)
  82. key = strconv.Itoa(int(it))
  83. case int16:
  84. it := value.(int16)
  85. key = strconv.Itoa(int(it))
  86. case uint16:
  87. it := value.(uint16)
  88. key = strconv.Itoa(int(it))
  89. case int32:
  90. it := value.(int32)
  91. key = strconv.Itoa(int(it))
  92. case uint32:
  93. it := value.(uint32)
  94. key = strconv.Itoa(int(it))
  95. case int64:
  96. it := value.(int64)
  97. key = strconv.FormatInt(it, 10)
  98. case uint64:
  99. it := value.(uint64)
  100. key = strconv.FormatUint(it, 10)
  101. case string:
  102. key = value.(string)
  103. case []byte:
  104. key = string(value.([]byte))
  105. default:
  106. newValue, _ := json.Marshal(value)
  107. key = string(newValue)
  108. }
  109. return key
  110. }