lib.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package lib
  2. import (
  3. "encoding/json"
  4. "github.com/nats-io/nats.go"
  5. "runtime"
  6. "strconv"
  7. "strings"
  8. )
  9. var Nats *nats.Conn
  10. type JSONS struct {
  11. //必须的大写开头
  12. Code int16
  13. Msg string
  14. Data interface{} // 泛型
  15. }
  16. func To_string(value interface{}) string {
  17. var key string
  18. if value == nil {
  19. return key
  20. }
  21. switch value.(type) {
  22. case float64:
  23. ft := value.(float64)
  24. key = strconv.FormatFloat(ft, 'f', -1, 64)
  25. case float32:
  26. ft := value.(float32)
  27. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  28. case int:
  29. it := value.(int)
  30. key = strconv.Itoa(it)
  31. case uint:
  32. it := value.(uint)
  33. key = strconv.Itoa(int(it))
  34. case int8:
  35. it := value.(int8)
  36. key = strconv.Itoa(int(it))
  37. case uint8:
  38. it := value.(uint8)
  39. key = strconv.Itoa(int(it))
  40. case int16:
  41. it := value.(int16)
  42. key = strconv.Itoa(int(it))
  43. case uint16:
  44. it := value.(uint16)
  45. key = strconv.Itoa(int(it))
  46. case int32:
  47. it := value.(int32)
  48. key = strconv.Itoa(int(it))
  49. case uint32:
  50. it := value.(uint32)
  51. key = strconv.Itoa(int(it))
  52. case int64:
  53. it := value.(int64)
  54. key = strconv.FormatInt(it, 10)
  55. case uint64:
  56. it := value.(uint64)
  57. key = strconv.FormatUint(it, 10)
  58. case string:
  59. key = value.(string)
  60. case []byte:
  61. key = string(value.([]byte))
  62. default:
  63. newValue, _ := json.Marshal(value)
  64. key = string(newValue)
  65. }
  66. return key
  67. }
  68. func To_int(value interface{}) int {
  69. var key int
  70. if value == nil {
  71. return key
  72. }
  73. switch value.(type) {
  74. case float64:
  75. key = int(value.(float64))
  76. case float32:
  77. key = int(value.(float32))
  78. case int:
  79. key = int(value.(int))
  80. case uint:
  81. key = int(value.(uint))
  82. case int8:
  83. key = int(value.(int8))
  84. case uint8:
  85. key = int(value.(uint8))
  86. case int16:
  87. key = int(value.(int16))
  88. case uint16:
  89. key = int(value.(uint16))
  90. case int32:
  91. key = int(value.(int32))
  92. case uint32:
  93. key = int(value.(uint32))
  94. case int64:
  95. key = int(value.(int64))
  96. case uint64:
  97. key = int(value.(uint64))
  98. case string:
  99. key, _ = strconv.Atoi(value.(string))
  100. case []byte:
  101. key, _ = strconv.Atoi(string(value.([]byte)))
  102. default:
  103. newValue, _ := json.Marshal(value)
  104. key, _ = strconv.Atoi(string(newValue))
  105. }
  106. return key
  107. }
  108. // 取文本(字符串)中间
  109. func GetBetweenStr(str, start, end string) string {
  110. n := strings.Index(str, start)
  111. if n == -1 {
  112. n = 0
  113. } else {
  114. n = n + len(start) // 增加了else,不加的会把start带上
  115. }
  116. str = string([]byte(str)[n:])
  117. m := strings.Index(str, end)
  118. if m == -1 {
  119. m = len(str)
  120. }
  121. str = string([]byte(str)[:m])
  122. return str
  123. }
  124. // 获取正在运行的函数名
  125. func FuncName() string {
  126. pc := make([]uintptr, 1)
  127. runtime.Callers(2, pc)
  128. f := runtime.FuncForPC(pc[0])
  129. return f.Name()
  130. }