GoLang遍历yaml文件

huangapple go评论71阅读模式
英文:

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:
  - &quot;pip install --upgrade pip&quot;
  - &quot;python3 --version&quot;

buildSteps:
  - &quot;pip install .&quot;

runProcess:
  - &quot;python3 test.py&quot;

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, &amp;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 == &quot;initSteps&quot; {
			s := reflect.ValueOf(v)
			for i := 0; i &lt; 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 (
	&quot;fmt&quot;
	&quot;github.com/spf13/viper&quot;
)

func main() {
	readYaml()
}

func readYaml() {
	viper.SetConfigName(&quot;test&quot;)                // name of config file (without extension)
	viper.SetConfigType(&quot;yaml&quot;)                // REQUIRED if the config file does not have the extension in the name
	viper.AddConfigPath(&quot;./Examples/readyaml&quot;) // 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(&quot;fatal error config file: %w&quot;, 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]

huangapple
  • 本文由 发表于 2022年6月28日 22:17:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/72788127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定