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:
-
why is it necessary to define each mapping in yaml like
yaml:"Name"
…it cant just map the first item to first key? -
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”