type.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 GetRandomString(l int) string {
  47. str := "0123456789"
  48. bytes := []byte(str)
  49. var result []byte
  50. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  51. for i := 0; i < l; i++ {
  52. result = append(result, bytes[r.Intn(len(bytes))])
  53. }
  54. return string(result)
  55. }
  56. func GetDayNum(inputType string, inputData int) (int, error) {
  57. result := 0
  58. switch inputType {
  59. case "year":
  60. if inputData < 1 {
  61. fmt.Println("年份错误!")
  62. break
  63. }
  64. result = inputData
  65. case "month":
  66. months := []int{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}
  67. if (inputData <= 12) && (inputData < 0) {
  68. fmt.Println("月份错误!")
  69. break
  70. }
  71. result = months[inputData-1]
  72. case "day":
  73. if (inputData < 0) && (inputData > 31) {
  74. fmt.Println("日期错误!")
  75. break
  76. }
  77. result = inputData
  78. default:
  79. return 0, fmt.Errorf("输入参数非法:%s", inputType)
  80. }
  81. return result, nil
  82. }
  83. // RemoveRepByMap 通过map主键唯一的特性过滤重复元素
  84. func RemoveRepByMap(slc []int64) []int64 {
  85. if len(slc) == 0 {
  86. return slc
  87. }
  88. var result []int64
  89. tempMap := map[int64]byte{} // 存放不重复主键
  90. for _, e := range slc {
  91. l := len(tempMap)
  92. tempMap[e] = 0
  93. if len(tempMap) != l { // 加入map后,map长度变化,则元素不重复
  94. result = append(result, e)
  95. }
  96. }
  97. return result
  98. }