golang yaml utils for common use
To get the package, execute:
go get github.com/gofika/yamlutil
package main
import (
"fmt"
"github.com/gofika/yamlutil"
)
type Foo struct {
Name string `yaml:"name"`
Value int `yaml:"value"`
}
func main() {
foo := &Foo{
Name: "Jason",
Value: 100,
}
name := "foo.yaml"
// write struct to file
err := yamlutil.WriteFile(name, foo)
if err != nil {
fmt.Printf("WriteFile failed. err: %s\n", err.Error())
return
}
// read struct from file
bar, err = yamlutil.ReadFile[Foo](name)
if err != nil {
fmt.Printf("ReadFile failed. err: %s\n", err.Error())
return
}
fmt.Printf("bar.Name: %s\n", bar.Name)
fmt.Printf("bar.Value: %d\n", bar.Value)
}