Question
How can I print the Id, Title, Name, and other fields of this struct to the console in Go?
type Project struct {
Id int64 `json:"project_id"`
Title string `json:"title"`
Name string `json:"name"`
Data Data `json:"data"`
Commits Commits `json:"commits"`
}
Short Answer
By the end of this page, you will understand how to print Go struct values to the console, how to access individual fields, and when to use fmt.Println, fmt.Printf, %v, %+v, and %#v for readable debugging output.
Concept
In Go, a struct is a custom data type that groups related values together. To print a struct in the console, you can either:
- print the entire struct at once, or
- print individual fields such as
project.Idorproject.Title
This is useful for:
- debugging
- logging program state
- checking API responses
- understanding nested data
Go uses the fmt package for formatted output. Different formatting verbs control how much detail you see:
%vprints values in a simple form%+vprints values and field names%#vprints Go-syntax representation
For structs, %+v is often the most helpful because it shows both field names and values.
The JSON tags in your struct, such as `json:"project_id"`, are used when encoding or decoding JSON. They do not change how fmt prints the struct.
Mental Model
Think of a struct like a labeled folder.
- The folder is
Project - Each document inside is a field:
Id,Title,Name,Data,Commits
You can either:
- hold up the whole folder and print everything at once, or
- pull out one document at a time and print only what you need
fmt.Println(project) is like showing the folder contents quickly.
fmt.Printf("%+v\n", project) is like showing the folder with labels attached to each item.
fmt.Println(project.Title) is like looking at one specific document in the folder.
Syntax and Examples
Print individual struct fields
package main
import "fmt"
type Project struct {
Id int64
Title string
Name string
}
func main() {
project := Project{
Id: 101,
Title: "Website Redesign",
Name: "Client Portal",
}
fmt.Println(project.Id)
fmt.Println(project.Title)
fmt.Println(project.Name)
}
This accesses each field directly using dot notation:
project.Idproject.Titleproject.Name
Print the whole struct
package main
import "fmt"
type Project struct {
Id int64
Title string
Name string
}
func main() {
project := Project{101, , }
fmt.Println(project)
}
Step by Step Execution
Consider this example:
package main
import "fmt"
type Project struct {
Id int64
Title string
Name string
}
func main() {
project := Project{
Id: 7,
Title: "API Migration",
Name: "Backend Service",
}
fmt.Printf("%+v\n", project)
fmt.Println(project.Title)
}
Step by step:
- The program defines a struct type named
Project. - Inside
main, it creates a variable namedproject. - That variable stores three field values:
Id = 7Title = "API Migration"Name = "Backend Service"
fmt.Printf("%+v\n", project)prints the entire struct with field names.fmt.Println(project.Title)prints only theTitlefield.
Real World Use Cases
Printing struct values is very common in real Go programs.
Debugging API responses
When you decode JSON into a struct, you often print the result to confirm the data loaded correctly.
fmt.Printf("%+v\n", project)
Logging important values
You may log selected fields rather than the whole struct.
fmt.Printf("Project %d: %s\n", project.Id, project.Title)
Inspecting nested data
If a struct contains other structs or slices, printing the full value helps you inspect the structure during development.
Command-line tools
CLI apps often print struct data in a human-readable format before saving, updating, or deleting records.
Testing
Developers print actual values during test debugging when expected and actual results do not match.
Real Codebase Usage
In real projects, developers usually do more than just call fmt.Println.
Print only the fields you need
Instead of dumping a large struct, print the most useful values.
fmt.Printf("Project ID=%d Title=%s\n", project.Id, project.Title)
This keeps logs readable.
Use %+v for debugging
During development, %+v is a fast way to inspect a struct.
fmt.Printf("project: %+v\n", project)
Use guard checks before printing nested values
If your struct contains pointers, make sure they are not nil before accessing fields.
if projectPtr == nil {
fmt.Println("project is nil")
return
}
fmt.Println(projectPtr.Title)
Format logs consistently
In larger codebases, developers often use structured logging libraries, but the habit starts with clear field-based output.
Print JSON when needed
If you want output that matches JSON field names, developers often marshal the struct to JSON instead of relying on fmt.
Common Mistakes
1. Expecting JSON tag names to appear in fmt output
Broken expectation:
fmt.Printf("%+v\n", project)
You might expect:
{project_id:1 title:Demo name:Test}
But Go prints struct field names, not JSON tags:
{Id:1 Title:Demo Name:Test}
If you need JSON-style output, use encoding/json.
2. Forgetting to create a struct value before printing
Broken code:
var project Project
fmt.Println(project.Title)
This is valid Go, but beginners may be confused because it prints the zero value:
0for numbers""for strings
Make sure the struct is filled with real data before printing.
3. Using the wrong formatting verb
Broken code:
fmt.Printf("%d\n", project)
Comparisons
| Approach | Example | Output style | Best use |
|---|---|---|---|
fmt.Println(project) | fmt.Println(project) | Simple struct values | Quick checks |
fmt.Printf("%v\n", project) | %v | Basic value output | General printing |
fmt.Printf("%+v\n", project) | %+v | Field names and values | Debugging structs |
fmt.Printf("%#v\n", project) | %#v | Go-syntax representation | Developer inspection |
Cheat Sheet
// Print one field
fmt.Println(project.Id)
fmt.Println(project.Title)
// Print the whole struct
fmt.Println(project)
// Print with formatting verbs
fmt.Printf("%v\n", project) // values only
fmt.Printf("%+v\n", project) // field names + values
fmt.Printf("%#v\n", project) // Go-syntax representation
// Custom output
fmt.Printf("Id: %d, Title: %s\n", project.Id, project.Title)
Rules to remember
- Use dot notation to access a field:
project.Name - JSON tags do not affect
fmtoutput %dis for integers,%sis for strings,%vis general-purpose%+vis great for printing structs during debugging- Use
json.MarshalIndentif you want JSON-style output
Useful JSON printing example
b, err := json.MarshalIndent(project, "", " ")
if err != nil {
fmt.Println("error:", err)
}
fmt.Println((b))
FAQ
How do I print a struct in Go?
Use fmt.Println(project) for simple output or fmt.Printf("%+v\n", project) to include field names.
How do I print a specific struct field in Go?
Access it with dot notation, such as project.Id or project.Title, then print it with fmt.Println or fmt.Printf.
Why are my JSON tag names not printed?
JSON tags are used by packages like encoding/json. The fmt package prints Go field names, not JSON tag names.
What is the best way to print a struct for debugging?
fmt.Printf("%+v\n", project) is usually the best quick debugging choice because it includes field names.
How do I print a Go struct as JSON?
Use json.Marshal or json.MarshalIndent, then print the resulting string.
Can I print nested structs too?
Yes. Printing the full struct also prints nested structs, although the output may be harder to read if the data is large.
What happens if a struct field has not been set?
Go prints the field's zero value, such as for integers or an empty string for strings.
Mini Project
Description
Build a small Go program that creates a Project struct, prints selected fields, prints the full struct for debugging, and also prints JSON output. This helps you practice the most common ways developers inspect struct data.
Goal
Create and print a Project value in multiple useful formats.
Requirements
- Define a
Projectstruct withId,Title, andNamefields. - Create one
Projectvalue with sample data. - Print each field individually.
- Print the whole struct using
%+v. - Print the same struct as pretty JSON.
Keep learning
Related questions
Check if a Value Exists in a Slice in Go
Learn how to check whether a value exists in a slice in Go, and why Go has no Python-style `in` operator for arrays or slices.
Concatenating Slices in Go with append
Learn how to concatenate two slices in Go using append and the ... operator, with examples, pitfalls, and practical usage.
Convert String to Integer in Go: Idiomatic Parsing with strconv.Atoi
Learn the idiomatic way to convert a string to an int in Go using strconv.Atoi, with examples, errors, and common mistakes.