Question

How to Print Struct Variables in Go

gostruct

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.Id or project.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:

  • %v prints values in a simple form
  • %+v prints values and field names
  • %#v prints 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.

Take Quiz

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.Id
  • project.Title
  • project.Name

Print the whole struct

package main

import "fmt"

type Project struct {
    Id    int64
    Title string
    Name  string
}

func main() {
    project := Project{101, "Website Redesign", "Client Portal"}

    fmt.Println(project)
}

Possible output:

{101 Website Redesign Client Portal}

Print the whole struct with field names

package main

import "fmt"

type Project struct {
    Id    int64
    Title string
    Name  string
}

func main() {
    project := Project{101, "Website Redesign", "Client Portal"}

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

Output:

{Id:101 Title:Website Redesign Name:Client Portal}

Print with custom formatting

package main

import "fmt"

type Project struct {
    Id    int64
    Title string
    Name  string
}

func main() {
    project := Project{101, "Website Redesign", "Client Portal"}

    fmt.Printf("Id: %d, Title: %s, Name: %s\n", project.Id, project.Title, project.Name)
}

Output:

Id: 101, Title: Website Redesign, Name: Client Portal

This is the best option when you want clean, controlled output.

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:

  1. The program defines a struct type named Project.
  2. Inside main, it creates a variable named project.
  3. That variable stores three field values:
    • Id = 7
    • Title = "API Migration"
    • Name = "Backend Service"
  4. fmt.Printf("%+v\n", project) prints the entire struct with field names.
  5. fmt.Println(project.Title) prints only the Title field.

Output:

{Id:7 Title:API Migration Name:Backend Service}
API Migration

This shows the difference between printing the full struct and printing a single field.

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.

Take Quiz

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.

b, _ := json.MarshalIndent(project, "", "  ")
fmt.Println(string(b))

This is especially useful when the struct came from an API.

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:

  • 0 for 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)

%d is for integers, not structs.

Use one of these instead:

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

4. Accessing fields on a nil pointer

Broken code:

var project *Project
fmt.Println(project.Title)

This causes a panic because project is nil.

Fix:

if project != nil {
    fmt.Println(project.Title)
}

5. Printing large nested structs without formatting

A deeply nested struct may be hard to read with plain fmt.Println.

Better options:

  • fmt.Printf("%+v\n", value)
  • json.MarshalIndent(...) for pretty JSON output

Comparisons

ApproachExampleOutput styleBest use
fmt.Println(project)fmt.Println(project)Simple struct valuesQuick checks
fmt.Printf("%v\n", project)%vBasic value outputGeneral printing
fmt.Printf("%+v\n", project)%+vField names and valuesDebugging structs
fmt.Printf("%#v\n", project)%#vGo-syntax representationDeveloper inspection
Print individual fieldsfmt.Println(project.Id)Only one valueClean user-facing output
Convert to JSONjson.MarshalIndent(project, "", " ")JSON-style outputAPIs and readable structured output

%v vs %+v vs %#v

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

Typical outputs:

{1 Demo Test}
{Id:1 Title:Demo Name:Test}
main.Project{Id:1, Title:"Demo", Name:"Test"}

If you are learning or debugging, %+v is usually the most useful.

Take Quiz

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 fmt output
  • %d is for integers, %s is for strings, %v is general-purpose
  • %+v is great for printing structs during debugging
  • Use json.MarshalIndent if you want JSON-style output

Useful JSON printing example

b, err := json.MarshalIndent(project, "", "  ")
if err != nil {
    fmt.Println("error:", err)
    return
}
fmt.Println(string(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 0 for integers or an empty string for strings.

Take Quiz

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 Project struct with Id, Title, and Name fields.
  • Create one Project value with sample data.
  • Print each field individually.
  • Print the whole struct using %+v.
  • Print the same struct as pretty JSON.
Take Quiz

Keep learning

Related questions

View more