英文:
GoLang iterate over yaml file
问题
我想知道在Golang中是否可以实现这种逻辑。我有一个yaml文件,内容如下:
initSteps:
- "pip install --upgrade pip"
- "python3 --version"
buildSteps:
- "pip install ."
runProcess:
- "python3 test.py"
我正在使用gopkg.in/yaml.v3
来遍历这些步骤。我有一个函数将这些指令放入一个类似下面的映射中:
func init() {
// 将yaml转换为步骤映射
f, err := ioutil.ReadFile(devrun)
if err != nil {
panic(err)
}
steps = make(map[interface{}]interface{})
err2 := yaml.Unmarshal(f, &steps)
if err2 != nil {
panic(err2)
}
}
在我的main
函数中,我使用以下代码来遍历值,如果键匹配的话:
for k, v := range steps {
if k == "initSteps" {
s := reflect.ValueOf(v)
for i := 0; i < s.Len(); i++ {
singleVertex := s.Index(i).Elem()
fmt.Println(singleVertex)
}
}
}
我想知道是否可以遍历steps
中的键,并且如何实现。这样我就可以通过判断k.initSteps
来使用。yaml文件中的键始终相同,唯一变化的是它们下面的步骤。
英文:
I wanted to know if this logic is possible in Golang. I have a yaml file as such:
initSteps:
- "pip install --upgrade pip"
- "python3 --version"
buildSteps:
- "pip install ."
runProcess:
- "python3 test.py"
And am using gopkg.in/yaml.v3
to iterate over the steps. I have a function below that puts the instructions into a map like this:
func init() {
// add map from yaml to steps
f, err := ioutil.ReadFile(devrun)
if err != nil {
panic(err)
}
steps = make(map[interface{}]interface{})
err2 := yaml.Unmarshal(f, &steps)
if err2 != nil {
panic(err2)
}
}
And in my main
function have this to iterate over the values if the key matches:
for k, v := range steps {
if k == "initSteps" {
s := reflect.ValueOf(v)
for i := 0; i < s.Len(); i++ {
singleVertex := s.Index(i).Elem()
fmt.Println(singleVertex)
}
}
}
I wanted to see if it was possible to iterate over the key from steps
though and how I would go about adding this in. This way I can if k.initSteps
: The keys from the yaml file will always be the same with the only thing changing being the steps under them.
答案1
得分: 1
你可以使用viper来读取你的.yaml配置文件。以下是一个简单的代码示例:
import (
"fmt"
"github.com/spf13/viper"
)
func main() {
readYaml()
}
func readYaml() {
viper.SetConfigName("test") // 配置文件的名称(不包含扩展名)
viper.SetConfigType("yaml") // 如果配置文件的名称中没有扩展名,则必须设置此项
viper.AddConfigPath("./Examples/readyaml") // 配置文件所在的路径
err := viper.ReadInConfig() // 查找并读取配置文件
if err != nil { // 处理读取配置文件时的错误
panic(fmt.Errorf("fatal error config file: %w", err))
}
fmt.Println(viper.AllKeys())
for _, i := range viper.AllKeys() {
fmt.Println(i, viper.Get(i))
}
}
输出结果:
[initsteps buildsteps runprocess]
initsteps [pip install --upgrade pip python3 --version]
buildsteps [pip install .]
runprocess [python3 test.py]
英文:
You can read your .yaml config file with viper. Simple code example:
import (
"fmt"
"github.com/spf13/viper"
)
func main() {
readYaml()
}
func readYaml() {
viper.SetConfigName("test") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("./Examples/readyaml") // path to look for the config file in
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}
fmt.Println(viper.AllKeys())
for _, i := range viper.AllKeys() {
fmt.Println(i, viper.Get(i))
}
}
Output:
[initsteps buildsteps runprocess]
initsteps [pip install --upgrade pip python3 --version]
buildsteps [pip install .]
runprocess [python3 test.py]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论