Outputting Structures in Go: From Beginner to Pro in 10 Minutes

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why is "just printing a structure" in Go not so simple?

\n\n

Imagine: you're writing in Go, created a beautiful User structure, filled it with data, and just want to take a look inside. You write fmt.Println(user) and see something like {Alex 25 true}. Informative? Not really. What if there are 15 fields? What if one of them is a nested structure? What if it's a slice of structures?

\n\n

The question "How to print struct variables in console?" on Stack Overflow has gathered thousands of views for a reason. Every beginner (and not only) in Golang encounters the fact that the standard output of structures is a boring string without field names. And when the project grows, debugging turns into guesswork.

\n\n

In this article, we will cover all ways to output structures to the Go console: from basic to advanced. You will learn how to output a structure beautifully, with field names, as JSON, and even how to write your own method for printing. Let's go!

\n\n

▎1. The Simplest Way: fmt.Println and Its Limitations

\n\n

Let's start with the basics. We have a structure:

\n\n
package main\n\nimport "fmt"\n\ntype User struct {\n    Name   string\n    Age    int\n    Active bool\n}\n\nfunc main() {\n    u := User{Name: "Anna", Age: 30, Active: true}\n    fmt.Println(u)\n}\n
\n\n

Output:\n

{Anna 30 true}

\n\n

It works, but it's uninformative. You don't see which value belongs to which field. If the structure has 10 fields, you'll be guessing what is what. Especially if the data type repeats (for example, two string fields in a row).

\n\n

Conclusion: fmt.Println is only suitable for a quick check when the structure is small and you remember the order of fields exactly.

\n\n

When is this useful?

\n
    \n
  • You are writing a one-off script.
  • \n
  • The structure contains 2-3 fields of different types.
  • \n
  • You use fmt.Println for logs where speed matters, not readability.
  • \n
\n\n

▎2. The Lifesaver %+v: Printing with Field Names

\n\n

This is perhaps the most popular answer on Stack Overflow. The %+v format in fmt.Printf outputs the structure with field names:

\n\n
fmt.Printf("%+v\\n", u)\n
\n\n

Output:\n

{Name:Anna Age:30 Active:true}

\n\n

Much better! You can clearly see where the name, age, and status are.

\n\n

What else is in the fmt arsenal?

\n
    \n
  • %v — standard representation (like in Println).
  • \n
  • %+v — with field names.
  • \n
  • %#v — full representation with package and type (see below).
  • \n
  • %T — outputs only the variable type.
  • \n
\n\n

Example with %#v:

\n
fmt.Printf("%#v\\n", u)\n// main.User{Name:"Anna", Age:30, Active:true}\n
\n\n

This format is useful when you want to copy the output and paste it back into code — it's a valid literal initialization of the structure.

\n\n

Tip: remember the combination %+v — it's your bread and butter when debugging structures in Go.

\n\n

▎3. Beauty and Order: Serialization to JSON

\n\n

When the structure is large, nested, or contains slices, %+v can become unreadable. This is where JSON comes to the rescue. Go can natively convert a structure to JSON:

\n\n
package main\n\nimport (\n    "encoding/json"\n    "fmt"\n)\n\ntype Address struct {\n    City  string `json:"city"`\n    Street string `json:"street"`\n}\n\ntype User struct {\n    Name    string  `json:"name"`\n    Age     int     `json:"age"`\n    Active  bool    `json:"active"`\n    Address Address `json:"address"`\n}\n\nfunc main() {\n    u := User{\n        Name:   "Ivan",\n        Age:    28,\n        Active: true,\n        Address: Address{\n            City:   "Moscow",\n            Street: "Tverskaya",\n        }                

Blogs

Book Recommendations