There is no official yaml library, but gopkg.in/yaml.v2
is a good choice. To unmarshal the given yaml you can define structs and add yaml tags to the attributes.
By using maps for your bridges and vlans and using arrays for the ports you can unmarshal the data without a problem.
As you are using maps, keep in mind that iterating over a map does not guarantee the order of returned elements.
This program would unmarshal your given structure:
package main
import (
"fmt"
"log"
yaml "gopkg.in/yaml.v2"
)
var data = `
controlling_bridge_1:
ip: "1.1.1.1"
ports: ["1","2"]
vlans:
vlan01:
name: "vlan1"
tag: 1001
ports: ["1"]
ip: "2.2.2.2"
vlan02:
name: "vlan02"
tag: 1002
ports: ["3", "4"]
ip: "3.3.3.1"
controlling_bridge_2:
ip: "1.1.1.1"
ports: ["1","2"]
vlans:
vlan01:
name: "vlan1"
tag: 1001
ports: ["1"]
ip: "2.2.2.2"
vlan02:
name: "vlan02"
tag: 1002
ports: ["3", "4"]
ip: "3.3.3.1"
`
type Bridge struct {
IP string `yaml:"ip"`
Ports []string `yaml:"ports"`
Vlans map[string]Vlan
}
type Vlan struct {
Name string `yaml:"name"`
Tag string `yaml:"tag"`
Ports []string `yaml:"ports"`
IP string `yaml:"ip"`
}
func main() {
bridges := map[string]Bridge{}
err := yaml.Unmarshal([]byte(data), &bridges)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("%+v
", bridges)
}