I'm trying to learn to use YAML v3 while marshaling a struct to config yaml.
Note: Update: My original example was little over simplified. My actual requirements is below:
For example, I have:
package main
import (
"fmt"
"io/ioutil"
"gopkg.in/yaml.v3"
)
type Ec2 struct {
Class string `yaml:"class"`
Ip string `yaml:"ip"`
}
type Vpc struct {
Subnet string `yaml:"subnet"`
Igw interface{} `yaml:"igw"`
}
type Config struct {
Ec2 Ec2 `yaml:"ec2"`
Vpc Vpc `yaml:"vpc"`
}
func main() {
c := Config{}
bytes, err := yaml.Marshal(c)
if err != nil {
fmt.Println(err)
}
ioutil.WriteFile("config.yaml", bytes, 0644)
}
Which create a file named config.yaml file in pwd like:
ec2:
class: ""
ip: ""
vpc:
subnet: ""
igw: null
But I am trying to generate config file (in this case config.yaml) like:
ec2:
# This section is for EC2
class: ""
ip: ""
vpc:
# This section is for VPC
subnet: ""
igw: null
Can this be achievable using V.3?