Skip to content

Instantly share code, notes, and snippets.

@dangminhtruong
Created August 10, 2019 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dangminhtruong/89f08843b58d54669099363843b6fec8 to your computer and use it in GitHub Desktop.
Save dangminhtruong/89f08843b58d54669099363843b6fec8 to your computer and use it in GitHub Desktop.
actions:
- run
- swim
- kick
person:
john:
name: John
info:
address: HaNoi
age: 26
phone: 777
skills:
- php
- golang
- nodejs
trump:
name: Trump
info:
address: SF
age: 27
phone: 888
skills:
- html
- javascript
- css
package main
import (
"fmt"
"io/ioutil"
"log"
"reflect"
yaml "gopkg.in/yaml.v2"
)
type Yml struct {
Actions []string
Person map[string]map[string]interface{}
}
func main() {
yml := Yml{}
yamlFile, err := ioutil.ReadFile("example.yml")
HandleError("Read yml file error -> ", err)
e := yaml.Unmarshal(yamlFile, &yml)
HandleError("Unmarshal error -> ", e)
//fmt.Println(yml.Person)
for _, v := range yml.Person {
for _, ps := range v {
i := reflect.ValueOf(ps)
if i.Kind() == reflect.Map {
for _, key := range i.MapKeys() {
strct := i.MapIndex(key)
fmt.Println(key.Interface(), strct.Interface())
}
} else if i.Kind() == reflect.Slice {
for t := 0; t < i.Len(); t++ {
fmt.Println(i.Index(t))
}
} else if i.Kind() == reflect.String {
fmt.Println(i)
}
}
}
}
func HandleError(message string, e error) {
if e != nil {
log.Fatal(message, e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment