type.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package util
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "reflect"
  6. "time"
  7. )
  8. // IsNull 判断是否为空字符串
  9. func IsNull(s string) string {
  10. if s == "" {
  11. return "0"
  12. }
  13. return s
  14. }
  15. // IsBlank 判断 reflect.Value 是否为空
  16. func IsBlank(value reflect.Value) bool {
  17. switch value.Kind() {
  18. case reflect.String:
  19. return value.Len() == 0
  20. case reflect.Bool:
  21. return !value.Bool()
  22. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  23. return value.Int() == 0
  24. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  25. return value.Uint() == 0
  26. case reflect.Float32, reflect.Float64:
  27. return value.Float() == 0
  28. case reflect.Interface, reflect.Ptr:
  29. return value.IsNil()
  30. }
  31. return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
  32. }
  33. func GetNumString(num int64) string {
  34. if num < 10000 {
  35. return fmt.Sprintf("%v.00", num)
  36. } else if num >= 10000 && num < 100000000 {
  37. mean := float32(num) / float32(10000)
  38. str := fmt.Sprintf("%.1f", mean)
  39. return str + "万"
  40. } else {
  41. mean := float32(num) / float32(100000000)
  42. str := fmt.Sprintf("%.1f", mean)
  43. return str + "亿"
  44. }
  45. }
  46. func GetTimePoionter(t *time.Time) time.Time {
  47. if t == nil {
  48. return time.Now()
  49. } else {
  50. return *t
  51. }
  52. }
  53. func GetRandomString(l int) string {
  54. str := "0123456789"
  55. bytes := []byte(str)
  56. var result []byte
  57. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  58. for i := 0; i < l; i++ {
  59. result = append(result, bytes[r.Intn(len(bytes))])
  60. }
  61. return string(result)
  62. }
  63. func GetDayNum(inputType string, inputData int) (int, error) {
  64. result := 0
  65. switch inputType {
  66. case "year":
  67. if inputData < 1 {
  68. fmt.Println("年份错误!")
  69. break
  70. }
  71. result = inputData
  72. case "month":
  73. months := []int{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}
  74. if (inputData <= 12) && (inputData < 0) {
  75. fmt.Println("月份错误!")
  76. break
  77. }
  78. result = months[inputData-1]
  79. case "day":
  80. if (inputData < 0) && (inputData > 31) {
  81. fmt.Println("日期错误!")
  82. break
  83. }
  84. result = inputData
  85. default:
  86. return 0, fmt.Errorf("输入参数非法:%s", inputType)
  87. }
  88. return result, nil
  89. }
  90. // RemoveRepByMap 通过map主键唯一的特性过滤重复元素
  91. func RemoveRepByMap(slc []int64) []int64 {
  92. if len(slc) == 0 {
  93. return slc
  94. }
  95. var result []int64
  96. tempMap := map[int64]byte{} // 存放不重复主键
  97. for _, e := range slc {
  98. l := len(tempMap)
  99. tempMap[e] = 0
  100. if len(tempMap) != l { // 加入map后,map长度变化,则元素不重复
  101. result = append(result, e)
  102. }
  103. }
  104. return result
  105. }
  106. func RemoveStrRepByMap(slc []string) []string {
  107. if len(slc) == 0 {
  108. return slc
  109. }
  110. var result []string
  111. tempMap := map[string]byte{} // 存放不重复主键
  112. for _, e := range slc {
  113. l := len(tempMap)
  114. tempMap[e] = 0
  115. if len(tempMap) != l { // 加入map后,map长度变化,则元素不重复
  116. result = append(result, e)
  117. }
  118. }
  119. return result
  120. }