Parsing YAML in Go 2020-12-19 · 3 min read Go YAML gopkg.in/yaml.v2Assuming config.yaml is in the same directory as your entry file. 123456TodoLists: - Test 1 - Test 2 - Test 3CloudKey: adasdx7817238123213 Here’s how to parse it into a struct: 123456789101112131415161718192021222324252627282930313233343536373839404142package mainimport ( "fmt" "io/ioutil" "gopkg.in/yaml.v2")// Define the structure to match the YAMLtype TodoLists struct { // Use tags to map YAML keys to struct fields Lists []string `yaml:"TodoLists"` CloudKey string `yaml:"CloudKey"`}func main(){ // Initialize the struct todolists := TodoLists{} filePath := "./config.yaml" buffer, err := ioutil.ReadFile(filePath) if err != nil { panic(err) } // Unmarshal the YAML data into the struct err = yaml.Unmarshal(buffer, &todolists) if err != nil { panic(err) } // Iterate and print the list for _, value := range todolists.Lists { fmt.Println(value) }}// Output:// Test 1// Test 2// Test 3