1

Go开发的数据条件验证工具,使用场景应该挺多

 1 year ago
source link: https://studygolang.com/articles/36048
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Go开发的数据条件验证工具,使用场景应该挺多

miaogaolin · 大约21小时之前 · 308 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览    

开源地址: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 
=~  // 包含
!= // 不包含

欢迎多提提问题!


有疑问加站长微信联系(非本文作者))

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK