Unmarshal a particular structure as string in Golang (yaml)

Viewed 189

Let's suppose I've a simple yaml like:-

info: []
return:
- foo.com:
    some_value:
        1.1.1.1: true
    some_value2:
        hey: hi

How do I get the value of foo.com as a string? I really don't care what structure is inside.

Using the popular library, gopkg.in/yaml.v2, I've tried a struct like this:-

type ExecuteJidResp struct {
    Info   []interface{}            `yaml:"info"`
    Return []map[string]interface{} `yaml:"return,omitempty"`
}

But, it errors out with:-

panic: yaml: line 4: found character that cannot start any token

Full code: https://play.golang.org/p/RJtQ9V9qGEY

1 Answers

In your case problem the rison of error

panic: yaml: line 4: found character that cannot start any token [recovered]
    panic: yaml: line 4: found character that cannot start any token

in yaml formatting:

yourBody := `info: []
return:
- foo.com:
    some_value:
        1.1.1.1: true
    some_value2:
        hey: hi`

rightBody := `info: []
return:
- foo.com:
    some_value:
      1.1.1.1: true
    some_value2:
      hey: hi`

Look at fixed result in playground

Related