Last modified: Jan 22, 2026 By Alexander Williams

Print Variable Values in Go: A Guide with Examples

Printing variable values is a fundamental skill. It is essential for debugging and understanding program flow. This guide covers all the methods.

Why Print Variables in Go?

You print variables to see their current state. This helps find bugs. It also helps verify logic. The fmt package provides the tools.

If you are new to Go, start with Your First Go Program: Hello World Tutorial. It introduces the basic structure.

Importing the fmt Package

First, import the fmt package. It is part of the standard library. You do not need to install it separately.


package main

import "fmt" // Import the formatting package

func main() {
    // Your code here
}
    

Using fmt.Print and fmt.Println

The fmt.Print function prints text without a newline. The fmt.Println function adds a newline at the end. They are simple and direct.


package main

import "fmt"

func main() {
    name := "Alice"
    age := 30

    fmt.Print("Name: ")
    fmt.Print(name)
    fmt.Print(", Age: ")
    fmt.Print(age)
    fmt.Print("\n") // Manually add newline

    fmt.Println("Name:", name, "Age:", age) // Automatically adds spaces and newline
}
    

Name: Alice, Age: 30
Name: Alice Age: 30
    

fmt.Println is great for quick output. It separates arguments with spaces.

Formatting Output with fmt.Printf

The fmt.Printf function gives you control. You use format verbs within a string. Variables are inserted where the verbs are.


package main

import "fmt"

func main() {
    product := "Laptop"
    price := 999.99
    quantity := 5

    // %s for string, %f for float, %d for integer
    fmt.Printf("Product: %s\n", product)
    fmt.Printf("Price: $%.2f\n", price) // Two decimal places
    fmt.Printf("Quantity: %d units\n", quantity)
    fmt.Printf("Total for %d x %s: $%.2f\n", quantity, product, float64(quantity)*price)
}
    

Product: Laptop
Price: $999.99
Quantity: 5 units
Total for 5 x Laptop: $4999.95
    

Common format verbs are %v (default format), %T (type), and %#v (Go syntax representation).

Common Format Verbs for Variables

Here is a quick reference for useful verbs.


package main

import "fmt"

func main() {
    val := 42
    str := "hello"
    pi := 3.14159

    fmt.Printf("Default %%v: %v\n", val)
    fmt.Printf("Type %%T: %T\n", str)
    fmt.Printf("Go syntax %%#v: %#v\n", str)
    fmt.Printf("Float %%f: %f\n", pi)
    fmt.Printf("Float with precision %%.2f: %.2f\n", pi)
    fmt.Printf("Boolean %%t: %t\n", true)
}
    

Default %v: 42
Type %T: string
Go syntax %#v: "hello"
Float %f: 3.141590
Float with precision %.2f: 3.14
Boolean %t: true
    

Printing Complex Data Types

You can print slices, maps, and structs easily. The %v verb works well for these.


package main

import "fmt"

func main() {
    // A slice of strings
    colors := []string{"red", "green", "blue"}
    fmt.Printf("Slice: %v\n", colors)

    // A map
    scores := map[string]int{"Alice": 95, "Bob": 87}
    fmt.Printf("Map: %v\n", scores)

    // A struct
    type Person struct {
        Name string
        Age  int
    }
    p := Person{Name: "Charlie", Age: 25}
    fmt.Printf("Struct: %v\n", p)
    fmt.Printf("Struct with fields: %+v\n", p) // Shows field names
}
    

Slice: [red green blue]
Map: map[Alice:95 Bob:87]
Struct: {Charlie 25}
Struct with fields: {Name:Charlie Age:25}
    

For more on slices, see Go List of Strings: Create, Iterate, Manipulate.

Storing Formatted Strings with fmt.Sprintf

Use fmt.Sprintf when you need the string, not just to print it. It formats and returns a string.


package main

import "fmt"

func main() {
    username := "dev_user"
    id := 1001

    // Create a formatted message string
    logMessage := fmt.Sprintf("User '%s' (ID: %d) logged in.", username, id)
    fmt.Println(logMessage)

    // You can use the string elsewhere
    fileName := fmt.Sprintf("report_%d.txt", id)
    fmt.Println("File to save:", fileName)
}
    

User 'dev_user' (ID: 1001) logged in.
File to save: report_1001.txt
    

This is useful for building log messages, file paths, or user output.

Debugging with Special Print Functions

The fmt package has functions for debugging. They print to standard error.


package main

import (
    "fmt"
    "os"
)

func main() {
    data := "sensitive"
    fmt.Fprintln(os.Stderr, "DEBUG: Processing data:", data) // Explicit stderr

    // fmt.Errorf creates an error with formatting
    err := fmt.Errorf("validation failed for value: %q", data)
    fmt.Println("Error:", err)
}
    

Using standard error keeps debug output separate from normal program output.

Best Practices for Printing Variables

Follow these tips for clean and effective printing.

  • Use fmt.Printf for complex, formatted output.
  • Use fmt.Println for simple, quick debugging.
  • Prefer %+v to print struct fields with names.
  • Use fmt.Sprintf to build strings for logging or UI.
  • Remember proper Go Variable Naming Rules for Beginners for readable code.

Conclusion

Printing variables in Go is straightforward. The fmt package is powerful. Use Println for simplicity. Use Printf for control. Use Sprintf for strings.

Master these functions. They are key to debugging and creating user-friendly output. They will help you understand your code as you build more complex applications.