package utils import ( "crypto/md5" "encoding/hex" "fmt" "os" "path/filepath" "strconv" "strings" uuid "github.com/satori/go.uuid" ) // Md5 md5加密算法 func Md5(s string) string { h := md5.New() h.Write([]byte(s)) cipherStr := h.Sum(nil) return fmt.Sprintf("%s", hex.EncodeToString(cipherStr)) } // CreateIfNotExist 创建目录 func CreateIfNotExist(filename string) error { dir := filepath.Dir(filename) if dir != "" { exists, _ := Exists(dir) if !exists { err := os.MkdirAll(dir, os.ModePerm) if err != nil { return err } } } return nil } // Exists ... func Exists(filename string) (bool, error) { exists := true _, err := os.Stat(filename) if err != nil { if os.IsNotExist(err) { exists = false } } return exists, err } // UUID uuid func UUID() string { s := uuid.NewV4() return s.String() } // CheckString 字符串比较 func CheckString(target string, value string, operator int) bool { switch operator { case 1: return target == value case 2: return target != value case 3: return strings.Contains(value, target) case 4: return !strings.Contains(value, target) } return false } // CheckInt 数值比较 func CheckInt(target, value, operator int) bool { switch operator { case 1: return value > target case 2: return value >= target case 3: return value == target case 4: return value <= target case 5: return value < target case 6: return value != target } return false } // CheckValue 目标值验证 func CheckValue(target string, value interface{}, fieldType, operator int) bool { var strValue string var intValue int switch value.(type) { case string: strValue = value.(string) case int: intValue = value.(int) case float64: floatValue := value.(float64) intValue = int(floatValue) default: break } switch fieldType { case 1: if strValue != "" { return CheckString(target, strValue, operator) } case 2: tInt, _ := strconv.Atoi(target) return CheckInt(tInt, intValue, operator) } return false }