lib.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package lib
  2. import (
  3. "encoding/json"
  4. "github.com/nats-io/nats.go"
  5. "github.com/thinkeridea/go-extend/exunicode/exutf8"
  6. "runtime"
  7. "strconv"
  8. "unicode/utf8"
  9. )
  10. var Nats *nats.Conn
  11. func To_int64(value interface{}) int64 {
  12. var key int64
  13. if value == nil {
  14. return key
  15. }
  16. switch value.(type) {
  17. case float64:
  18. key = int64(value.(float64))
  19. case float32:
  20. key = int64(value.(float32))
  21. case int:
  22. key = int64(value.(int))
  23. case uint:
  24. key = int64(value.(uint))
  25. case int8:
  26. key = int64(value.(int8))
  27. case uint8:
  28. key = int64(value.(uint8))
  29. case int16:
  30. key = int64(value.(int16))
  31. case uint16:
  32. key = int64(value.(uint16))
  33. case int32:
  34. key = int64(value.(int32))
  35. case uint32:
  36. key = int64(value.(uint32))
  37. case int64:
  38. key = int64(value.(int64))
  39. case uint64:
  40. key = int64(value.(uint64))
  41. case string:
  42. key, _ = strconv.ParseInt(value.(string), 10, 64)
  43. case []byte:
  44. key, _ = strconv.ParseInt(string(value.([]byte)), 10, 64)
  45. default:
  46. newValue, _ := json.Marshal(value)
  47. key, _ = strconv.ParseInt(string(newValue), 10, 64)
  48. }
  49. return key
  50. }
  51. func To_string(value interface{}) string {
  52. var key string
  53. if value == nil {
  54. return key
  55. }
  56. switch value.(type) {
  57. case float64:
  58. ft := value.(float64)
  59. key = strconv.FormatFloat(ft, 'f', -1, 64)
  60. case float32:
  61. ft := value.(float32)
  62. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  63. case int:
  64. it := value.(int)
  65. key = strconv.Itoa(it)
  66. case uint:
  67. it := value.(uint)
  68. key = strconv.Itoa(int(it))
  69. case int8:
  70. it := value.(int8)
  71. key = strconv.Itoa(int(it))
  72. case uint8:
  73. it := value.(uint8)
  74. key = strconv.Itoa(int(it))
  75. case int16:
  76. it := value.(int16)
  77. key = strconv.Itoa(int(it))
  78. case uint16:
  79. it := value.(uint16)
  80. key = strconv.Itoa(int(it))
  81. case int32:
  82. it := value.(int32)
  83. key = strconv.Itoa(int(it))
  84. case uint32:
  85. it := value.(uint32)
  86. key = strconv.Itoa(int(it))
  87. case int64:
  88. it := value.(int64)
  89. key = strconv.FormatInt(it, 10)
  90. case uint64:
  91. it := value.(uint64)
  92. key = strconv.FormatUint(it, 10)
  93. case string:
  94. key = value.(string)
  95. case []byte:
  96. key = string(value.([]byte))
  97. default:
  98. newValue, _ := json.Marshal(value)
  99. key = string(newValue)
  100. }
  101. return key
  102. }
  103. // 获取正在运行的函数名
  104. func FuncName() string {
  105. pc := make([]uintptr, 1)
  106. runtime.Callers(2, pc)
  107. f := runtime.FuncForPC(pc[0])
  108. return f.Name()
  109. }
  110. func Limit_len(str string,lenx int) string {
  111. if utf8.RuneCountInString(str) > lenx {
  112. return exutf8.RuneSubString(str, 0, lenx-3)+"..."
  113. }
  114. return exutf8.RuneSubString(str, 0, lenx)
  115. }