Go json Marshal
Simple example of a Go json Unmarshal
and Marshal
, because
I keep needing this…
package main
import (
"encoding/json"
"fmt"
"os"
)
func main() {
type ColorGroup struct {
ID int
Name string
Colors []string
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
var groupResult ColorGroup
err = json.Unmarshal(b, &groupResult)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", groupResult)
}