25% off

Use code FUNCMAIN at checkout for 25% off all premium courses.

Get started →
Pretty Printing JSON Structs in Go
Code Snippet

Pretty Printing JSON Structs in Go

go

👋 Welcome Gophers! In this snippet, we are going to look at how you can pretty-print JSON in Go using the json package!

In this example we have a simple struct called Flight which we want to pretty print the values for.

We can use the json.MarshalIndent function which takes in the struct we want to marshal as well as the prefix string and the indent string. In this example we don’t use a prefix, but we do set the indent to 2 empty spaces.

Further Reading:

If you found this snippet useful, you may also like some of the other snippets on the site: