Created
February 25, 2014 04:44
-
-
Save masahide/9202832 to your computer and use it in GitHub Desktop.
go で yaml 等を「map[interface{}]interface{}」型で読み込んだ際の動的型の参照方法 ref: http://qiita.com/yamasaki-masahide/items/d6e406c4c11d5870a1c6
This file contains hidden or 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
mail_from: [email protected] | |
to: [[email protected]] | |
var: | |
c: 2 | |
d: [3, 4] |
This file contains hidden or 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
m := make(map[interface{}]interface{}) | |
err = goyaml.Unmarshal([]byte(config), &m) | |
if err != nil { | |
panic(err) | |
} | |
//fmt.Printf("--- m:\n%# v\n\n", m) | |
pretty.Printf("--- m:\n%# v\n\n", m) |
This file contains hidden or 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
$ go run main.go | |
--- m: | |
map[interface {}]interface {}{ | |
"mail_from": "[email protected]", | |
"to": []interface {}{ | |
"[email protected]", | |
}, | |
"var": map[interface {}]interface {}{ | |
"c": int(2), | |
"d": []interface {}{ | |
int(3), | |
int(4), | |
}, | |
}, | |
} |
This file contains hidden or 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
fmt.Printf("--- m.to:\n%# v\n\n", m["to"]) |
This file contains hidden or 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
fmt.Printf("--- m.var.c:\n%# v\n\n", m["var"]["c"]) |
This file contains hidden or 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
fmt.Printf("--- m.var.c:\n%# v\n\n", m["var"].(map[interface {}]interface {})["c"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment