golang json utils for common use
To get the package, execute:
go get github.com/gofika/jsonutil
package main
import (
"fmt"
"github.com/gofika/jsonutil"
)
type Foo struct {
Name string `json:"name"`
Value int `json:"value"`
}
func main() {
foo := &Foo{
Name: "Jason",
Value: 100,
}
name := "foo.json"
// write struct to file
err := jsonutil.WriteFile(name, foo)
if err != nil {
fmt.Printf("WriteFile failed. err: %s\n", err.Error())
return
}
// read struct from file
bar, err = jsonutil.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)
}