Created
November 5, 2021 06:13
-
-
Save palnabarun/5e773f8e9e62b467e7cf12affe6591f1 to your computer and use it in GitHub Desktop.
Experiment to parse YAML comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# some comment | |
- name: John # Pal | |
city: Hogwarts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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