Quick actions

cmd+k|ctrl+k

Navigation

Languages

Essential Go / JSON

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T10:02:12Z

Updated

2019-03-26T10:02:12Z

package main

import (
	"encoding/json"
	"fmt"
	"log"
)

type Person struct {
	fullName string
	Name     string
	Age      int    `json:"age"`
	City     string `json:"city"`
}

func main() {
	p := Person{
		Name: "John",
		Age:  37,
		City: "SF",
	}
	d, err := json.Marshal(&p)
	if err != nil {
		log.Fatalf("json.MarshalIndent failed with '%s'\n", err)
	}
	fmt.Printf("Person in compact JSON: %s\n", string(d))

	d, err = json.MarshalIndent(p, "", "  ")
	if err != nil {
		log.Fatalf("json.MarshalIndent failed with '%s'\n", err)
	}
	fmt.Printf("Person in pretty-printed JSON:\n%s\n", string(d))
}
INFO