-1

Why wouldn't the following Go program work? The map is empty after the Unmarshal. Is there any changes that I could do to deserialize this correctly? Or should I end up using yaml.Node?

package main

import (
    "fmt"
    "gopkg.in/yaml.v3"
)

func main() {
    empYaml := `
      employees:
        - id: 11
          name: Irshad
          department: IT
          designation: Product Manager
          address:
            city: Mumba
            state: Maharashtra
            country: India
    `

    var result map[string]interface{}
    yaml.Unmarshal([]byte(empYaml), &result)

    fmt.Println(result)
}

Play URL: https://play.golang.org/p/tG44j15mNjH

Charles Prakash Dasari
  • 4,964
  • 1
  • 27
  • 46

1 Answers1

2

When I check the error from the call to yaml.Unmarshal I see the error:

2009/11/10 23:00:00 yaml: line 2: found character that cannot start any token

Looks like this may be due to tab characters in the content since YAML forbids tabs.

Remove all tab characters from the YAML content and you should be in good shape. I get the following output:

map[employees:[map[address:map[city:Mumba country:India state:Maharashtra] department:IT designation:Product Manager id:11 name:Irshad]]]
James
  • 4,599
  • 2
  • 19
  • 27