Created
August 10, 2019 10:20
-
-
Save dangminhtruong/89f08843b58d54669099363843b6fec8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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