util.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package util
  4. import (
  5. "errors"
  6. "math"
  7. "os"
  8. "path/filepath"
  9. "strconv"
  10. )
  11. // SplitPath 将路径按分隔符分隔成字符串数组。比如:
  12. // /a/b/c ==> []string{"a", "b", "c"}
  13. func SplitPath(path string) []string {
  14. vol := filepath.VolumeName(path)
  15. ret := make([]string, 0, 10)
  16. index := 0
  17. if len(vol) > 0 {
  18. ret = append(ret, vol)
  19. path = path[len(vol)+1:]
  20. }
  21. for i := 0; i < len(path); i++ {
  22. if os.IsPathSeparator(path[i]) {
  23. if i > index {
  24. ret = append(ret, path[index:i])
  25. }
  26. index = i + 1 // 过滤掉此符号
  27. }
  28. } // end for
  29. if len(path) > index {
  30. ret = append(ret, path[index:])
  31. }
  32. return ret
  33. }
  34. // Round 返回浮点数四舍五入后的整数
  35. func Round(v float64) int64 {
  36. return int64(math.Round(v))
  37. }
  38. // MaxInt64 返回int64最大值
  39. func MaxInt64(x int, y int) int {
  40. if x >= y {
  41. return x
  42. }
  43. return y
  44. }
  45. // MinInt64 返回int64最小值
  46. func MinInt64(x int, y int) int {
  47. if x <= y {
  48. return x
  49. }
  50. return y
  51. }
  52. // MaxInt 返回int最大值
  53. func MaxInt(x int, y int) int {
  54. if x >= y {
  55. return x
  56. }
  57. return y
  58. }
  59. // MinInt 返回int最小值
  60. func MinInt(x int, y int) int {
  61. if x <= y {
  62. return x
  63. }
  64. return y
  65. }
  66. // IdCertGender 身份证的性别
  67. // 返回:1 => 男性,0 => 女性
  68. func IdCertGender(idcert string) (int, error) {
  69. if !IsIdCert(idcert) {
  70. return 0, errors.New("invalid idcert")
  71. }
  72. v, err := strconv.Atoi(idcert[17:17])
  73. if err != nil {
  74. return 0, err
  75. }
  76. return v / 2, nil
  77. }