gpt4 book ai didi

string - 如何读取外部变量

转载 作者:行者123 更新时间:2023-12-01 22:33:25 24 4
gpt4 key购买 nike

我正在尝试读取--configFile=""作为外部参数发送的变量,但是即使我通过了绝对路径,也总是找不到错误
Vars:

var (
c Config
conf = c.getConf()
app = kingpin.New("exporter", "Exporter for Prometheus.")
configFile = app.Flag("configFile", "Configuration file destination (/etc/exporter/config.yaml)").Default("/etc/exporter/config.yaml").String()
)
配置:
func (c *Config) getConf() *Config {

yamlFile, err := ioutil.ReadFile(string(*configFile))
if err != nil {
log.Errorf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Errorf("Unmarshal: %v", err)
}
return c
}
命令/输出:
server:/etc/exporter # ./exporter --configFile="/etc/exporter/config.yaml"
ERRO[0000] yamlFile.Get err #open : no such file or directory
ls -ltr:
total 14152
-rw------- 1 root root 1334 Sep 25 20:47 config.yaml
-rwxrwxr-x 1 root root 14466568 Sep 25 22:03 exporter

最佳答案

在此处替换代码https://github.com/rafal-szypulka/itm_exporter/blob/master/main.go#L51
与:

conf *Config
在此处替换代码 https://github.com/rafal-szypulka/itm_exporter/blob/master/main.go#L354
与:
p := kingpin.MustParse(app.Parse(os.Args[1:]))
conf = c.getConf()
switch p {

关于string - 如何读取外部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64073012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com