golang 的 rabbitmq 消费项目
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

373 рядки
6.7 KiB

  1. package utils
  2. import (
  3. "encoding/binary"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "math"
  8. "strconv"
  9. "strings"
  10. )
  11. func ToString(raw interface{}, e error) (res string) {
  12. if e != nil {
  13. return ""
  14. }
  15. return AnyToString(raw)
  16. }
  17. func ToInt64(raw interface{}, e error) int64 {
  18. if e != nil {
  19. return 0
  20. }
  21. return AnyToInt64(raw)
  22. }
  23. func Float64ToStrByPrec(f float64, prec int) string {
  24. return strconv.FormatFloat(f, 'f', prec, 64)
  25. }
  26. func Float64ToStrPrec4(f float64) string {
  27. return strconv.FormatFloat(f, 'f', 4, 64)
  28. }
  29. func StrToFormat(c *gin.Context, s string, prec int) string {
  30. ex := strings.Split(s, ".")
  31. if len(ex) == 2 {
  32. if StrToFloat64(ex[1]) == 0 && c.GetString("is_show_point") != "1" { //小数点后面为空就是不要小数点了
  33. return ex[0]
  34. }
  35. //看取多少位
  36. str := ex[1]
  37. str1 := str
  38. if prec < len(str) {
  39. str1 = str[0:prec]
  40. } else {
  41. for i := 0; i < prec-len(str); i++ {
  42. str1 += "0"
  43. }
  44. }
  45. if prec > 0 {
  46. return ex[0] + "." + str1
  47. } else {
  48. return ex[0]
  49. }
  50. }
  51. return s
  52. }
  53. // 强制舍弃尾数
  54. func Truncate(num string, prec int) string {
  55. if num == "" {
  56. return ""
  57. }
  58. if prec >= len(num) {
  59. return num
  60. }
  61. newn := strings.Split(num, ".")
  62. if len(newn) < 2 || prec >= len(newn[1]) {
  63. return num
  64. }
  65. return newn[0] + "." + newn[1][:prec]
  66. }
  67. func AnyToBool(raw interface{}) bool {
  68. switch i := raw.(type) {
  69. case float32, float64, int, int64, uint, uint8, uint16, uint32, uint64, int8, int16, int32:
  70. return i != 0
  71. case []byte:
  72. return i != nil
  73. case string:
  74. if i == "false" {
  75. return false
  76. }
  77. return i != ""
  78. case error:
  79. return false
  80. case nil:
  81. return true
  82. }
  83. val := fmt.Sprint(raw)
  84. val = strings.TrimLeft(val, "&")
  85. if strings.TrimLeft(val, "{}") == "" {
  86. return false
  87. }
  88. if strings.TrimLeft(val, "[]") == "" {
  89. return false
  90. }
  91. // ptr type
  92. b, err := json.Marshal(raw)
  93. if err != nil {
  94. return false
  95. }
  96. if strings.TrimLeft(string(b), "\"\"") == "" {
  97. return false
  98. }
  99. if strings.TrimLeft(string(b), "{}") == "" {
  100. return false
  101. }
  102. return true
  103. }
  104. func AnyToInt64(raw interface{}) int64 {
  105. switch i := raw.(type) {
  106. case string:
  107. res, _ := strconv.ParseInt(i, 10, 64)
  108. return res
  109. case []byte:
  110. return BytesToInt64(i)
  111. case int:
  112. return int64(i)
  113. case int64:
  114. return i
  115. case uint:
  116. return int64(i)
  117. case uint8:
  118. return int64(i)
  119. case uint16:
  120. return int64(i)
  121. case uint32:
  122. return int64(i)
  123. case uint64:
  124. return int64(i)
  125. case int8:
  126. return int64(i)
  127. case int16:
  128. return int64(i)
  129. case int32:
  130. return int64(i)
  131. case float32:
  132. return int64(i)
  133. case float64:
  134. return int64(i)
  135. case error:
  136. return 0
  137. case bool:
  138. if i {
  139. return 1
  140. }
  141. return 0
  142. }
  143. return 0
  144. }
  145. func AnyToString(raw interface{}) string {
  146. switch i := raw.(type) {
  147. case []byte:
  148. return string(i)
  149. case int:
  150. return strconv.FormatInt(int64(i), 10)
  151. case int64:
  152. return strconv.FormatInt(i, 10)
  153. case float32:
  154. return Float64ToStr(float64(i))
  155. case float64:
  156. return Float64ToStr(i)
  157. case uint:
  158. return strconv.FormatInt(int64(i), 10)
  159. case uint8:
  160. return strconv.FormatInt(int64(i), 10)
  161. case uint16:
  162. return strconv.FormatInt(int64(i), 10)
  163. case uint32:
  164. return strconv.FormatInt(int64(i), 10)
  165. case uint64:
  166. return strconv.FormatInt(int64(i), 10)
  167. case int8:
  168. return strconv.FormatInt(int64(i), 10)
  169. case int16:
  170. return strconv.FormatInt(int64(i), 10)
  171. case int32:
  172. return strconv.FormatInt(int64(i), 10)
  173. case string:
  174. return i
  175. case error:
  176. return i.Error()
  177. case bool:
  178. return strconv.FormatBool(i)
  179. }
  180. return fmt.Sprintf("%#v", raw)
  181. }
  182. func AnyToFloat64(raw interface{}) float64 {
  183. switch i := raw.(type) {
  184. case []byte:
  185. f, _ := strconv.ParseFloat(string(i), 64)
  186. return f
  187. case int:
  188. return float64(i)
  189. case int64:
  190. return float64(i)
  191. case float32:
  192. return float64(i)
  193. case float64:
  194. return i
  195. case uint:
  196. return float64(i)
  197. case uint8:
  198. return float64(i)
  199. case uint16:
  200. return float64(i)
  201. case uint32:
  202. return float64(i)
  203. case uint64:
  204. return float64(i)
  205. case int8:
  206. return float64(i)
  207. case int16:
  208. return float64(i)
  209. case int32:
  210. return float64(i)
  211. case string:
  212. f, _ := strconv.ParseFloat(i, 64)
  213. return f
  214. case bool:
  215. if i {
  216. return 1
  217. }
  218. }
  219. return 0
  220. }
  221. func ToByte(raw interface{}, e error) []byte {
  222. if e != nil {
  223. return []byte{}
  224. }
  225. switch i := raw.(type) {
  226. case string:
  227. return []byte(i)
  228. case int:
  229. return Int64ToBytes(int64(i))
  230. case int64:
  231. return Int64ToBytes(i)
  232. case float32:
  233. return Float32ToByte(i)
  234. case float64:
  235. return Float64ToByte(i)
  236. case uint:
  237. return Int64ToBytes(int64(i))
  238. case uint8:
  239. return Int64ToBytes(int64(i))
  240. case uint16:
  241. return Int64ToBytes(int64(i))
  242. case uint32:
  243. return Int64ToBytes(int64(i))
  244. case uint64:
  245. return Int64ToBytes(int64(i))
  246. case int8:
  247. return Int64ToBytes(int64(i))
  248. case int16:
  249. return Int64ToBytes(int64(i))
  250. case int32:
  251. return Int64ToBytes(int64(i))
  252. case []byte:
  253. return i
  254. case error:
  255. return []byte(i.Error())
  256. case bool:
  257. if i {
  258. return []byte("true")
  259. }
  260. return []byte("false")
  261. }
  262. return []byte(fmt.Sprintf("%#v", raw))
  263. }
  264. func Int64ToBytes(i int64) []byte {
  265. var buf = make([]byte, 8)
  266. binary.BigEndian.PutUint64(buf, uint64(i))
  267. return buf
  268. }
  269. func BytesToInt64(buf []byte) int64 {
  270. return int64(binary.BigEndian.Uint64(buf))
  271. }
  272. func StrToInt(s string) int {
  273. res, _ := strconv.Atoi(s)
  274. return res
  275. }
  276. func StrToInt64(s string) int64 {
  277. res, _ := strconv.ParseInt(s, 10, 64)
  278. return res
  279. }
  280. func Float32ToByte(float float32) []byte {
  281. bits := math.Float32bits(float)
  282. bytes := make([]byte, 4)
  283. binary.LittleEndian.PutUint32(bytes, bits)
  284. return bytes
  285. }
  286. func ByteToFloat32(bytes []byte) float32 {
  287. bits := binary.LittleEndian.Uint32(bytes)
  288. return math.Float32frombits(bits)
  289. }
  290. func Float64ToByte(float float64) []byte {
  291. bits := math.Float64bits(float)
  292. bytes := make([]byte, 8)
  293. binary.LittleEndian.PutUint64(bytes, bits)
  294. return bytes
  295. }
  296. func ByteToFloat64(bytes []byte) float64 {
  297. bits := binary.LittleEndian.Uint64(bytes)
  298. return math.Float64frombits(bits)
  299. }
  300. func Float64ToStr(f float64) string {
  301. return strconv.FormatFloat(f, 'f', 2, 64)
  302. }
  303. func Float64ToStrPrec1(f float64) string {
  304. return strconv.FormatFloat(f, 'f', 1, 64)
  305. }
  306. func Float64ToStrPrec6(f float64) string {
  307. return strconv.FormatFloat(f, 'f', 6, 64)
  308. }
  309. func Float32ToStr(f float32) string {
  310. return Float64ToStr(float64(f))
  311. }
  312. func StrToFloat64(s string) float64 {
  313. res, err := strconv.ParseFloat(s, 64)
  314. if err != nil {
  315. return 0
  316. }
  317. return res
  318. }
  319. func StrToFloat32(s string) float32 {
  320. res, err := strconv.ParseFloat(s, 32)
  321. if err != nil {
  322. return 0
  323. }
  324. return float32(res)
  325. }
  326. func StrToBool(s string) bool {
  327. b, _ := strconv.ParseBool(s)
  328. return b
  329. }
  330. func BoolToStr(b bool) string {
  331. if b {
  332. return "true"
  333. }
  334. return "false"
  335. }
  336. func FloatToInt64(f float64) int64 {
  337. return int64(f)
  338. }
  339. func IntToStr(i int) string {
  340. return strconv.Itoa(i)
  341. }
  342. func Int64ToStr(i int64) string {
  343. return strconv.FormatInt(i, 10)
  344. }