Avatar ·

How to pretty print JSON in Go: convenient formatting for debugging and logs

Hello everyone! I just started learning Go and encountered a problem when working with JSON. I have a struct that I serialize to JSON, but when I output the result via fmt.Println, everything is displayed as a single line without formatting. This is very inconvenient for debugging, especially when the struct is large and nested. Here's an example of my code: ```go package main import ( "encoding/json" "fmt" ) type User struct { Name string `json:"name"` Age int `json:"age"` Hobbies []string `json:"hobbies"` Address struct { City string `json:"city"` Zip string `json:"zip"` } `json:"address"` } func main() { u := User{ Name: "Alice", Age: 30, Hobbies: []string{"reading", "coding", "hiking"}, } ```