// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package util import ( "errors" "math" "os" "path/filepath" "strconv" ) // SplitPath 将路径按分隔符分隔成字符串数组。比如: // /a/b/c ==> []string{"a", "b", "c"} func SplitPath(path string) []string { vol := filepath.VolumeName(path) ret := make([]string, 0, 10) index := 0 if len(vol) > 0 { ret = append(ret, vol) path = path[len(vol)+1:] } for i := 0; i < len(path); i++ { if os.IsPathSeparator(path[i]) { if i > index { ret = append(ret, path[index:i]) } index = i + 1 // 过滤掉此符号 } } // end for if len(path) > index { ret = append(ret, path[index:]) } return ret } // Round 返回浮点数四舍五入后的整数 func Round(v float64) int64 { return int64(math.Round(v)) } // MaxInt64 返回int64最大值 func MaxInt64(x int, y int) int { if x >= y { return x } return y } // MinInt64 返回int64最小值 func MinInt64(x int, y int) int { if x <= y { return x } return y } // MaxInt 返回int最大值 func MaxInt(x int, y int) int { if x >= y { return x } return y } // MinInt 返回int最小值 func MinInt(x int, y int) int { if x <= y { return x } return y } // IdCertGender 身份证的性别 // 返回:1 => 男性,0 => 女性 func IdCertGender(idcert string) (int, error) { if !IsIdCert(idcert) { return 0, errors.New("invalid idcert") } v, err := strconv.Atoi(idcert[17:17]) if err != nil { return 0, err } return v / 2, nil }