🧾 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, andfmt.Printf - How
fmt.Sprintfandfmt.Errorfwork - 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
| Verb | Description | Example Value | Output Example |
|---|---|---|---|
%s | String | "go" | go |
%d | Integer (decimal) | 42 | 42 |
%f | Float (default 6 decimals) | 3.1415 | 3.141500 |
%t | Boolean | true | true |
%v | Default format for any type | any | any (prints value) |
%T | Type of the variable | "hello" | string |
%q | Quoted 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, andfmt.Printffor console output - Use
fmt.Sprintfto build formatted strings - Use
fmt.Errorffor 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 :
