golang-im聊天
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

370 líneas
6.6 KiB

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