Go - yaml help. Parse value for internships is empty

package main

import (
	"fmt"
	"io/ioutil"
	"log"

	"gopkg.in/yaml.v2"
)

type instanceConfig struct {
	Name    string `yaml:"Name"`
	Address string `yaml:"Address"`
	Phone   int    `yaml:"Phone"`
	WFH     bool

	Internships string `yaml:"Internships"`
}

func (c *instanceConfig) Parse(data []byte) error {
	return yaml.Unmarshal(data, c)
}

func main() {
	data, err := ioutil.ReadFile("file.yml")
	if err != nil {
		log.Fatal(err)
	}
	var config instanceConfig
	if err := config.Parse(data); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", config)
}

I am trying to parse yaml in GO. Two problems:

  1. why is it necessary to define each mapping in yaml like yaml:"Name" …it cant just map the first item to first key?

  2. how can I output the Internships as a list within the yaml? Adding 'Internships list yaml:"Internships"’ did not work at all.


    Name: “Arka”
    Address: “India”
    Phone: 100
    WorkFromHome: true
    Mail: “abc@hotmail.com

    Internships:
    - JournalDev
    - MNO
    - ABC
    - XYZ
    Education:
    MSc: 7.6
    BSc: 8.1
    XII: 9
    Courses:
    Udemy: 15
    Coursera: 3
    References: “Mr. QWERTY, XYZ”

For built-in JSON lib it is the way you describe, perhaps the author of the yaml decoder hasn’t implemented it (yet).

Internships []string `yaml:"Internships"` should totally do. At least that is how it works in the built-in JSON decoder.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.