Go Getting Started
Estimated reading: 3 minutes 319 views

Go Fmt Package – Format & Print Data with fmt in Go (2025 Guide)

Introduction – What Is the fmt Package in Go?

The fmt package in Go is part of its standard library, and it provides functions for formatted I/O operations. It’s one of the most used packages in Go, essential for printing output to the console, formatting strings, and even scanning input.

In this section, you’ll learn:

  • How to use fmt.Print, fmt.Println, and fmt.Printf
  • How fmt.Sprintf and fmt.Errorf work
  • Format verbs (%s, %d, %f, etc.)
  • The difference between printing and formatting-only functions

Importing the fmt Package

Before using any fmt functions, you must import the package:

import "fmt"

All formatting functions (printing, scanning, string building) are accessed through this import.


Output Functions in fmt

fmt.Print()

Prints arguments as a single line, without a newline at the end.

fmt.Print("Hello, ")
fmt.Print("Go!")

Output:

Hello, Go!

fmt.Println()

Prints arguments with spaces between them and adds a newline at the end.

fmt.Println("Welcome", "to", "Go!")

Output:

Welcome to Go!

fmt.Printf()

Supports formatted output using verbs like %s, %d, %f, etc.

name := "Alice"
age := 30
fmt.Printf("%s is %d years old\n", name, age)

Output:

Alice is 30 years old

Common Format Verbs in fmt

VerbDescriptionExample ValueOutput Example
%sString"go"go
%dInteger (decimal)4242
%fFloat (default 6 decimals)3.14153.141500
%tBooleantruetrue
%vDefault format for any typeanyany (prints value)
%TType of the variable"hello"string
%qQuoted string"hi""hi"
%%Literal percent sign%%

String Formatting with fmt

fmt.Sprintf()

Formats a string and returns it without printing.

greeting := fmt.Sprintf("Hi, %s!", "Go Dev")
fmt.Println(greeting)

Output:

Hi, Go Dev!

fmt.Errorf()

Creates a custom error message using format verbs.

err := fmt.Errorf("Invalid input: %d", 404)
fmt.Println(err)

Output:

Invalid input: 404

Real-World Example

package main

import (
    "fmt"
)

func main() {
    name := "John"
    age := 25
    isMember := true

    fmt.Print("Welcome, ")
    fmt.Println(name)

    fmt.Printf("Name: %s\nAge: %d\nMembership: %t\n", name, age, isMember)
}

Output:

Welcome, John
Name: John
Age: 25
Membership: true

Summary – Recap & Next Steps

The fmt package is Go’s powerful tool for formatting and displaying text. From simple messages to structured logs and error formatting, it’s foundational to any Go program.

Key Takeaways:

  • Use fmt.Print, fmt.Println, and fmt.Printf for console output
  • Use fmt.Sprintf to build formatted strings
  • Use fmt.Errorf for formatted error messages
  • Format verbs give precision over how data is presented

Next: Learn about Go’s zero values, how uninitialized variables behave, and how to use them effectively.


FAQs – Go fmt Package

What’s the difference between Print and Println in fmt?
Print() outputs without a newline; Println() adds a newline and separates arguments with spaces.

When should I use Sprintf?
Use Sprintf() when you want to store formatted strings in variables or pass them without printing.

Can fmt format numbers with precision?
Yes. For example, %.2f will format a float to 2 decimal places.

What does %v do in fmt?
%v prints values in their default format. It’s a shortcut for unknown types.

Is fmt used only for printing?
No. It’s also used for formatting strings and generating custom error messages.


Share Now :
Share

Go – Fmt Package

Or Copy Link

CONTENTS
Scroll to Top