为了账号安全,请及时绑定邮箱和手机立即绑定

在 golang 中解组 json 时如何跳过现有字段?

在 golang 中解组 json 时如何跳过现有字段?

Go
繁花如伊 2022-12-19 10:35:19
我使用jsonfile 来配置我的程序参数,并使用flagpackage 来配置相同的参数。当一些参数同时被json文件解析flag时,我希望使用通过flag.麻烦的是json文件路径也是通过.解析出来的flag。调用后可以得到json路径flag.parse(),但是参数也被解析了,那么Unmarshaljson会覆盖flag解析的参数。示例 JSON:{    "opt1": 1,    "opt2": "hello"}示例代码:var Config = struct {    Opt1 int    `json:"opt1"`    Opt2 string `json:"opt2"`}{    Opt1: 0,    Opt2: "none",}func main() {    // parse config file path    var configFile string    flag.StringVar(&configFile, "config", "", "config file path")    // parse options    flag.IntVar(&Config.Opt1, "opt1", Config.Opt1, "")    flag.StringVar(&Config.Opt2, "opt2", Config.Opt2, "")    // parse flags    flag.Parse()    // load config options from config.json file    if configFile != "" {        if data, err := ioutil.ReadFile(configFile); err != nil {            fmt.Printf("read config file error: %v\n", err)        } else if err = json.Unmarshal(data, &Config); err != nil {            fmt.Printf("parse config file error: %v\n", err)        }    }    fmt.Printf("%+v", Config)}程序示例输出:./foo.exe -opt2 worldout:{Opt1:0 Opt2:world}./foo.exe -config config.json出去:{Opt1:1 Opt2:hello}./foo.exe -config config.json -opt2 world真正的出局:{Opt1:1 Opt2:hello}希望出局:{Opt1:1 Opt2:world}
查看完整描述

1 回答

?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

一个简单的解决方案是首先解析 JSON 配置文件,完成后,再继续解析 CLI 参数。


问题在于 JSON 配置文件也是 CLI arg。


flag.Parse()最简单的解决方案是在解析配置文件后再次调用:


// load config options from config.json file

if configFile != "" {

    if data, err := ioutil.ReadFile(configFile); err != nil {

        fmt.Printf("read config file error: %v\n", err)

    } else if err = json.Unmarshal(data, &Config); err != nil {

        fmt.Printf("parse config file error: %v\n", err)

    }


    // parse flags again to have precedence

    flag.Parse()

}

第二个flag.Parse()将覆盖并因此优先于来自 JSON 文件的数据。


加上这个,运行


./foo.exe -config config.json -opt2 world

输出将是你想要的:


{Opt1:1 Opt2:world}


查看完整回答
反对 回复 2022-12-19
  • 1 回答
  • 0 关注
  • 133 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号