Skip to content

Instantly share code, notes, and snippets.

@masahide
Created February 25, 2014 04:44
Show Gist options
  • Save masahide/9202832 to your computer and use it in GitHub Desktop.
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
mail_from: [email protected]
to: [[email protected]]
var:
c: 2
d: [3, 4]
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)
$ 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),
},
},
}
fmt.Printf("--- m.to:\n%# v\n\n", m["to"])
fmt.Printf("--- m.var.c:\n%# v\n\n", m["var"]["c"])
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