Go 常用数值比较判断

参考文章:Go 常用数值比较判断

使用 go 开发时,遇到对某个数据依据某个标准规则进行对比,得出结论
如:无法判断、正常、低于正常值、高于正常值。
以下及实现过程,通过正则匹配对应的判断规则,从而对提供的数值进行判断

compare.go

package main

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"
)

const (
	// 结论#0%无法判断|1%正常|2%低于正常值|3%高于正常值
	ResultUnknown = iota
	ResultNormal
	ResultSubnormal
	ResultAboveNormal
)

var substrArr = map[string]struct {
	// 正则表达式
	Pattern string
	// 比较形式#1%等于|2%小于|3%大于|4%小于等于|5%大于等于|6%范围
	Compare   int8
	Separator string
	Example   string
}{
	"=": {
		Pattern:   `^=[+-]?\d+(\.\d*)?$`,
		Compare:   1,
		Separator: "=",
		Example:   "=10%a|8|10|12",
	},
	"<": {
		Pattern:   `^<[+-]?\d+(\.\d*)?$`,
		Compare:   2,
		Separator: "<",
		Example:   "<10%a|8|10|12",
	},
	">": {
		Pattern:   `^>[+-]?\d+(\.\d*)?$`,
		Compare:   3,
		Separator: ">",
		Example:   ">10%a|8|10|12",
	},
	"≤": {
		Pattern:   `^≤[+-]?\d+(\.\d*)?$`,
		Compare:   4,
		Separator: "=<",
		Example:   "≤10%a|8|10|12",
	},
	"≥": {
		Pattern:   `^≥[+-]?\d+(\.\d*)?$`,
		Compare:   5,
		Separator: ">=",
		Example:   "≥10%a|8|10|12",
	},
	"-": {
		Pattern:   `^[+-]?\d+(\.\d*)?-[+-]?\d+(\.\d*)?$`,
		Compare:   6,
		Separator: "-",
		Example:   "18.5-24%a|16|18.5|20|24|26",
	},
	"~": {
		Pattern:   `^[+-]?\d+(\.\d*)?~[+-]?\d+(\.\d*)?$`,
		Compare:   6,
		Separator: "~",
		Example:   "18.5~24%a|16|18.5|20|24|26",
	},
}

func CompareResult(resultStr, referralStr string) int8 {
	var (
		separator string
		compare   int8
	)

	resultStr = strings.Trim(resultStr, " ")
	referralStr = strings.Trim(referralStr, " ")

	floatRes, errRes := strconv.ParseFloat(resultStr, 64)
	if errRes != nil {
		fmt.Println("result: 无法判断")
		fmt.Println("floatRes1 error", errRes)
		return ResultUnknown
	}

	for key, val := range substrArr {
		matched, err := regexp.MatchString(val.Pattern, referralStr)
		if err != nil || !matched {
			continue
		}
		compare = substrArr[key].Compare
		separator = key
	}

	index := strings.Index(referralStr, separator)
	min := referralStr[:index]
	max := referralStr[index+len(separator):]
	floatMin, errMin := strconv.ParseFloat(min, 64)
	floatMax, errMax := strconv.ParseFloat(max, 64)

	//fmt.Printf("referralStr:%s, resultStr:%s, separator: %s \n", referralStr, resultStr, separator)
	//fmt.Printf("compare: %d, index: %d, min: %s, max: %s \n", compare, index, min, max)
	//fmt.Printf("floatRes: %f, floatMin: %f, floatMax: %f \n", floatRes, floatMin, floatMax)

	// "="
	if compare == 1 {
		//fmt.Println("compare:1 =")
		if errMax != nil {
			fmt.Println("result: 无法判断")
			fmt.Println("floatMax2 error", errMax)
			return ResultUnknown
		}
		if floatRes == floatMax {
			return ResultNormal
		} else if floatRes < floatMax {
			return ResultSubnormal
		} else if floatRes > floatMax {
			return ResultAboveNormal
		}
	}

	// "<"
	if compare == 2 {
		//fmt.Println("compare:2 <")
		if errMax != nil {
			fmt.Println("result: 无法判断")
			fmt.Println("floatMax2 error", errMax)
			return ResultUnknown
		}
		if floatRes < floatMax {
			return ResultNormal
		} else {
			return ResultAboveNormal
		}
	}

	// ">"
	if compare == 3 {
		//fmt.Println("compare:3 >")
		if errMax != nil {
			fmt.Println("result: 无法判断")
			fmt.Println("floatMax2 error", errMax)
			return ResultUnknown
		}
		if floatRes > floatMax {
			return ResultNormal
		} else {
			return ResultSubnormal
		}
	}

	// "=<"
	if compare == 4 {
		//fmt.Println("compare:4 =<")
		if errMax != nil {
			fmt.Println("result: 无法判断")
			fmt.Println("floatMax2 error", errMax)
			return ResultUnknown
		}
		if floatRes <= floatMax {
			return ResultNormal
		} else {
			return ResultAboveNormal
		}
	}

	// ">="
	if compare == 5 {
		//fmt.Println("compare:5 >=")
		if errMax != nil {
			fmt.Println("result: 无法判断")
			fmt.Println("floatMax2 error", errMax)
			return ResultUnknown
		}
		if floatRes >= floatMax {
			return ResultNormal
		} else {
			return ResultSubnormal
		}
	}

	// - ~
	if compare == 6 {
		//fmt.Println("compare:6 -|~")
		if errMin != nil || errMax != nil {
			fmt.Println("result: 无法判断")
			fmt.Println("floatMax2 error", errMax, errMin)
			return ResultUnknown
		}
		if floatRes < floatMin {
			return ResultSubnormal
		} else if floatRes > floatMax {
			return ResultAboveNormal
		} else {
			return ResultNormal
		}
	}

	return ResultUnknown
}

compare_test.go

package main

import "testing"

func TestCompareResult(t *testing.T) {

	strRes := " 10 "
	str := " 10 "
	// 无法判断
	if result := CompareResult(strRes, str); result != ResultUnknown {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes1 := " a "
	str1 := " =10 "
	// 无法判断
	if result := CompareResult(strRes1, str1); result != ResultUnknown {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes1 = " 10 "
	str1 = " =10 "
	if result := CompareResult(strRes1, str1); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes1 = " 8 "
	str1 = " =10 "
	if result := CompareResult(strRes1, str1); result != ResultSubnormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes1 = " 12 "
	str1 = " =10 "
	if result := CompareResult(strRes1, str1); result != ResultAboveNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes2 := " a "
	str2 := " <10 "
	if result := CompareResult(strRes2, str2); result != ResultUnknown {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes2 = " 8 "
	str2 = " <10 "
	if result := CompareResult(strRes2, str2); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes2 = " 10 "
	str2 = " <10 "
	if result := CompareResult(strRes2, str2); result != ResultAboveNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes2 = " 12 "
	str2 = " <10 "
	if result := CompareResult(strRes2, str2); result != ResultAboveNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes3 := " a "
	str3 := " >10 "
	if result := CompareResult(strRes3, str3); result != ResultUnknown {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes3 = " 12 "
	str3 = " >10 "
	if result := CompareResult(strRes3, str3); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes3 = " 10 "
	str3 = " >10 "
	if result := CompareResult(strRes3, str3); result != ResultSubnormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes3 = " 8 "
	str3 = " >10 "
	if result := CompareResult(strRes3, str3); result != ResultSubnormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes4 := " a "
	str4 := " ≤10 "
	if result := CompareResult(strRes4, str4); result != ResultUnknown {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes4 = " 8 "
	str4 = " ≤10 "
	if result := CompareResult(strRes4, str4); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes4 = " 10 "
	str4 = " ≤10 "
	if result := CompareResult(strRes4, str4); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes4 = " 12 "
	str4 = " ≤10 "
	if result := CompareResult(strRes4, str4); result != ResultAboveNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes5 := " a "
	str5 := " ≥10 "
	if result := CompareResult(strRes5, str5); result != ResultUnknown {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes5 = " 10 "
	str5 = " ≥10 "
	if result := CompareResult(strRes5, str5); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes5 = " 12 "
	str5 = " ≥10 "
	if result := CompareResult(strRes5, str5); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes5 = " 8 "
	str5 = " ≥10 "
	if result := CompareResult(strRes5, str5); result != ResultSubnormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes6 := " a "
	str6 := " 18.5~24 "
	if result := CompareResult(strRes6, str6); result != ResultUnknown {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes6 = " 18.5 "
	str6 = " 18.5~24 "
	if result := CompareResult(strRes6, str6); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes6 = " 20 "
	str6 = " 18.5~24 "
	if result := CompareResult(strRes6, str6); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes6 = " 24 "
	str6 = " 18.5~24 "
	if result := CompareResult(strRes6, str6); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes6 = " 16 "
	str6 = " 18.5~24 "
	if result := CompareResult(strRes6, str6); result != ResultSubnormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes6 = " 26 "
	str6 = " 18.5~24 "
	if result := CompareResult(strRes6, str6); result != ResultAboveNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes7 := " a "
	str7 := " 18.5-24 "
	if result := CompareResult(strRes7, str7); result != ResultUnknown {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes7 = " 18.5 "
	str7 = " 18.5-24 "
	if result := CompareResult(strRes7, str7); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes7 = " 20 "
	str7 = " 18.5-24 "
	if result := CompareResult(strRes7, str7); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes7 = " 24 "
	str7 = " 18.5-24 "
	if result := CompareResult(strRes7, str7); result != ResultNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes7 = " 16 "
	str7 = " 18.5-24 "
	if result := CompareResult(strRes7, str7); result != ResultSubnormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}

	strRes7 = " 26 "
	str7 = " 18.5-24 "
	if result := CompareResult(strRes7, str7); result != ResultAboveNormal {
		t.Errorf("CompareResult failure. result: %d", result)
	}
}

测试结果

=== RUN   TestCompareResult
result: 无法判断
floatRes1 error strconv.ParseFloat: parsing "a": invalid syntax
result: 无法判断
floatRes1 error strconv.ParseFloat: parsing "a": invalid syntax
result: 无法判断
floatRes1 error strconv.ParseFloat: parsing "a": invalid syntax
result: 无法判断
floatRes1 error strconv.ParseFloat: parsing "a": invalid syntax
result: 无法判断
floatRes1 error strconv.ParseFloat: parsing "a": invalid syntax
result: 无法判断
floatRes1 error strconv.ParseFloat: parsing "a": invalid syntax
result: 无法判断
floatRes1 error strconv.ParseFloat: parsing "a": invalid syntax
--- PASS: TestCompareResult (0.00s)
PASS

Process finished with exit code 0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值