Parse Yaml to map[string]string

3,839 views
Skip to first unread message

Tong Sun

unread,
Mar 13, 2016, 8:46:01 AM3/13/16
to golang-nuts
Hi, 

If I have a Yaml file having the data in the following format, 

var data = `
hostname: 127.0.0.1
username: vagrant
ssh_key: "/long/path/to/private_key"
port: '2222'
last_action: create
`

How can I parse them to the map[string]string format? 


I.e., I can either parse the Yaml to an explicit struct mapping definition, like, 


type instanceConfig struct { Hostname string
Username string
SSHKey string `yaml:"ssh_key"`
Port string
}

Or to map[string]string but with a root node. 

Please help. 
Thanks

Tapio Raevaara

unread,
Mar 13, 2016, 2:23:55 PM3/13/16
to golang-nuts
m := map[string]string
err := yaml.Unmarshal([]byte(data), m)

Tong Sun

unread,
Mar 13, 2016, 2:31:11 PM3/13/16
to Tapio Raevaara, golang-nuts
Oh, yeah, didn't realize that I can do without a struct. 
Thanks

On Sun, Mar 13, 2016 at 2:23 PM, Tapio Raevaara wrote:
m := map[string]string
err := yaml.Unmarshal([]byte(data), m)

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/Wa838LaSM0g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages