// Chapter 35 example: turning a slice of structs into JSON and back. // This program has no server, and writes no file. // Run with: go run exercises/35/03-json-basics.go package main import ( "encoding/json" "fmt" ) type note struct { Text string } func main() { notes := []note{{Text: "Buy rice"}, {Text: "Call Arif"}} data, err := json.MarshalIndent(notes, "", " ") if err != nil { fmt.Println("Error:", err) return } fmt.Println(string(data)) var loaded []note err = json.Unmarshal(data, &loaded) if err != nil { fmt.Println("Error:", err) return } fmt.Println(loaded) err = json.Unmarshal([]byte("this is not JSON"), &loaded) if err != nil { fmt.Println("Error:", err) } }