开源地址:https://github.com/miaogaolin/condition
使用
package main
import (
"fmt"
"github.com/miaogaolin/condition"
)
func main() {
data := map[string]interface{}{
"col1": 1,
"col2": "hello world",
"col3": "male",
}
res, err := condition.Validate(data, `({col1}==1 and {col2} =~ "world") or {col3} in ["male"]`)
if err != nil {
panic(err)
}
if res {
fmt.Println("success")
}
}
如果在验证的数据不是 Map 类型,是 Struct 类型,推荐使用 https://github.com/fatih/structs 将 Struct 转为 Map。
关键字
==
>
<
>=
<=
!=
in
not in
=~ // 包含
!= // 不包含
欢迎多提提问题!
有疑问加站长微信联系(非本文作者))

看看这个 github.com/antonmedv/expr