如何在 Golang 中创建可重用代码以读取不同的 yaml 并将它们放入不同的结构类型中
How to create a reusable code in Golang to read different yamls and put them into different structs types
我必须阅读 2 个或 3 个或更多结构不同的 yaml,并且每个结构都有一个结构,我想将它们存储在其中。到目前为止,我正在为每个函数创建单独的函数并且它可以工作,但看起来不是很优雅......我认为。
以下是今天的函数:
// read the Yaml into struct(s)
type Config struct {...}
type ExecuteQueries struct {...}
func parseYamlConfig(pathYaml string) Config {
myConfig := Config{}
var err error
var yamlFile []byte
if pathYaml == "" {
yamlFile, err = ioutil.ReadFile("./conf/conf.yaml")
} else {
yamlFile, err = ioutil.ReadFile(pathYaml)
}
if err != nil {
log.Fatalf("error: %v", err)
}
err = yaml.Unmarshal([]byte(yamlFile), &myConfig)
if err != nil {
log.Fatalf("error: %v", err)
}
return myConfig
}
func parseYamlConfig2(pathYaml string) ExecuteQueries {
myConfig := ExecuteQueries{}
var err error
var yamlFile []byte
if pathYaml == "" {
yamlFile, err = ioutil.ReadFile("./conf/conf.yaml")
} else {
yamlFile, err = ioutil.ReadFile(pathYaml)
}
if err != nil {
log.Fatalf("error: %v", err)
}
err = yaml.Unmarshal([]byte(yamlFile), &myConfig)
if err != nil {
log.Fatalf("error: %v", err)
}
return myConfig
}
注意,其实他们return和接收的东西不一样,但是对数据的处理很相似。应该怎么表达更优雅?
func unmarshalYAMLFile(path string, v interface{}) error {
if path == "" {
path = "./conf/conf.yaml"
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return yaml.NewDecoder(f).Decode(v)
}
conf1 := Config{}
if err := unmarshalYAMLFile("/path/to/conf.yaml", &conf1); err != nil {
panic(err)
}
conf2 := ExecuteQueries{}
if err := unmarshalYAMLFile("/path/to/conf_2.yaml", &conf2); err != nil {
panic(err)
}
我必须阅读 2 个或 3 个或更多结构不同的 yaml,并且每个结构都有一个结构,我想将它们存储在其中。到目前为止,我正在为每个函数创建单独的函数并且它可以工作,但看起来不是很优雅......我认为。
以下是今天的函数:
// read the Yaml into struct(s)
type Config struct {...}
type ExecuteQueries struct {...}
func parseYamlConfig(pathYaml string) Config {
myConfig := Config{}
var err error
var yamlFile []byte
if pathYaml == "" {
yamlFile, err = ioutil.ReadFile("./conf/conf.yaml")
} else {
yamlFile, err = ioutil.ReadFile(pathYaml)
}
if err != nil {
log.Fatalf("error: %v", err)
}
err = yaml.Unmarshal([]byte(yamlFile), &myConfig)
if err != nil {
log.Fatalf("error: %v", err)
}
return myConfig
}
func parseYamlConfig2(pathYaml string) ExecuteQueries {
myConfig := ExecuteQueries{}
var err error
var yamlFile []byte
if pathYaml == "" {
yamlFile, err = ioutil.ReadFile("./conf/conf.yaml")
} else {
yamlFile, err = ioutil.ReadFile(pathYaml)
}
if err != nil {
log.Fatalf("error: %v", err)
}
err = yaml.Unmarshal([]byte(yamlFile), &myConfig)
if err != nil {
log.Fatalf("error: %v", err)
}
return myConfig
}
注意,其实他们return和接收的东西不一样,但是对数据的处理很相似。应该怎么表达更优雅?
func unmarshalYAMLFile(path string, v interface{}) error {
if path == "" {
path = "./conf/conf.yaml"
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return yaml.NewDecoder(f).Decode(v)
}
conf1 := Config{}
if err := unmarshalYAMLFile("/path/to/conf.yaml", &conf1); err != nil {
panic(err)
}
conf2 := ExecuteQueries{}
if err := unmarshalYAMLFile("/path/to/conf_2.yaml", &conf2); err != nil {
panic(err)
}