Skip to content

Instantly share code, notes, and snippets.

@palnabarun
Created November 5, 2021 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palnabarun/5e773f8e9e62b467e7cf12affe6591f1 to your computer and use it in GitHub Desktop.
Save palnabarun/5e773f8e9e62b467e7cf12affe6591f1 to your computer and use it in GitHub Desktop.
Experiment to parse YAML comments
# some comment
- name: John # Pal
city: Hogwarts
package main
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
type Person struct {
Name string `yaml:"name"`
City string `yaml:"city"`
}
func main() {
people := []Person{}
bytes, err := os.ReadFile("a.yaml")
if err != nil {
panic(err)
}
err = yaml.Unmarshal(bytes, &people)
if err != nil {
panic(err)
}
fmt.Printf("%+v", people)
d, err := yaml.Marshal(&people)
if err != nil {
panic(err)
}
err = os.WriteFile("b.yaml", d, 0644)
if err != nil {
panic(err)
}
}
package main
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
type Person struct {
Name yaml.Node `yaml:"name"`
City yaml.Node `yaml:"city"`
}
func main() {
people := []yaml.Node{}
bytes, err := os.ReadFile("a.yaml")
if err != nil {
panic(err)
}
err = yaml.Unmarshal(bytes, &people)
if err != nil {
panic(err)
}
fmt.Printf("%+v", people)
d, err := yaml.Marshal(&people)
if err != nil {
panic(err)
}
err = os.WriteFile("b.yaml", d, 0644)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment