convert.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
  2. // 🤖 Github Repository: https://github.com/gofiber/fiber
  3. // 📌 API Documentation: https://docs.gofiber.io
  4. package utils
  5. import (
  6. "fmt"
  7. "reflect"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // CopyString copies a string to make it immutable
  13. func CopyString(s string) string {
  14. return string(UnsafeBytes(s))
  15. }
  16. // CopyBytes copies a slice to make it immutable
  17. func CopyBytes(b []byte) []byte {
  18. tmp := make([]byte, len(b))
  19. copy(tmp, b)
  20. return tmp
  21. }
  22. const (
  23. uByte = 1 << (10 * iota) // 1 << 10 == 1024
  24. uKilobyte
  25. uMegabyte
  26. uGigabyte
  27. uTerabyte
  28. uPetabyte
  29. uExabyte
  30. )
  31. // ByteSize returns a human-readable byte string of the form 10M, 12.5K, and so forth.
  32. // The unit that results in the smallest number greater than or equal to 1 is always chosen.
  33. func ByteSize(bytes uint64) string {
  34. unit := ""
  35. value := float64(bytes)
  36. switch {
  37. case bytes >= uExabyte:
  38. unit = "EB"
  39. value /= uExabyte
  40. case bytes >= uPetabyte:
  41. unit = "PB"
  42. value /= uPetabyte
  43. case bytes >= uTerabyte:
  44. unit = "TB"
  45. value /= uTerabyte
  46. case bytes >= uGigabyte:
  47. unit = "GB"
  48. value /= uGigabyte
  49. case bytes >= uMegabyte:
  50. unit = "MB"
  51. value /= uMegabyte
  52. case bytes >= uKilobyte:
  53. unit = "KB"
  54. value /= uKilobyte
  55. case bytes >= uByte:
  56. unit = "B"
  57. default:
  58. return "0B"
  59. }
  60. result := strconv.FormatFloat(value, 'f', 1, 64)
  61. result = strings.TrimSuffix(result, ".0")
  62. return result + unit
  63. }
  64. // ToString Change arg to string
  65. func ToString(arg interface{}, timeFormat ...string) string {
  66. tmp := reflect.Indirect(reflect.ValueOf(arg)).Interface()
  67. switch v := tmp.(type) {
  68. case int:
  69. return strconv.Itoa(v)
  70. case int8:
  71. return strconv.FormatInt(int64(v), 10)
  72. case int16:
  73. return strconv.FormatInt(int64(v), 10)
  74. case int32:
  75. return strconv.FormatInt(int64(v), 10)
  76. case int64:
  77. return strconv.FormatInt(v, 10)
  78. case uint:
  79. return strconv.Itoa(int(v))
  80. case uint8:
  81. return strconv.FormatInt(int64(v), 10)
  82. case uint16:
  83. return strconv.FormatInt(int64(v), 10)
  84. case uint32:
  85. return strconv.FormatInt(int64(v), 10)
  86. case uint64:
  87. return strconv.FormatInt(int64(v), 10)
  88. case string:
  89. return v
  90. case []byte:
  91. return string(v)
  92. case bool:
  93. return strconv.FormatBool(v)
  94. case float32:
  95. return strconv.FormatFloat(float64(v), 'f', -1, 32)
  96. case float64:
  97. return strconv.FormatFloat(v, 'f', -1, 64)
  98. case time.Time:
  99. if len(timeFormat) > 0 {
  100. return v.Format(timeFormat[0])
  101. }
  102. return v.Format("2006-01-02 15:04:05")
  103. case reflect.Value:
  104. return ToString(v.Interface(), timeFormat...)
  105. case fmt.Stringer:
  106. return v.String()
  107. default:
  108. return ""
  109. }
  110. }