🖨️ Go Output – Display Data Using Print Functions in Go (2025 Guide)
🧲 Introduction – How Go Prints to the Screen
Displaying output is one of the first tasks you’ll perform in any programming language. In Go, output is handled using the fmt package — a powerful standard library module that supports formatted printing, basic debugging, and standard input/output operations.
🎯 In this section, you’ll learn:
- How to use
fmt.Print,fmt.Println, andfmt.Printf - Differences between print functions
- Formatting placeholders for strings, integers, floats, and more
- Real examples with output
📦 Importing the fmt Package
Before using any output functions, you need to import the fmt package at the top of your Go file:
import "fmt"
🔠 Basic Output Functions in Go
✅ fmt.Print()
Prints text without a newline at the end.
fmt.Print("Hello")
fmt.Print(" World")
📤 Output:
Hello World
✅ fmt.Println()
Prints text with a newline at the end.
fmt.Println("Hello")
fmt.Println("World")
📤 Output:
Hello
World
✅ fmt.Printf()
Used for formatted output, similar to printf in C/C++.
name := "Alice"
age := 30
fmt.Printf("%s is %d years old\n", name, age)
📤 Output:
Alice is 30 years old
🧾 Format Specifiers for Printf()
| Specifier | Description | Example Output |
|---|---|---|
%s | String | "Go" → Go |
%d | Integer (decimal) | 25 → 25 |
%f | Float (default 6 decimals) | 3.14159 → 3.141590 |
%t | Boolean | true → true |
%v | Any value (default format) | 42 → 42, Go → Go |
%T | Print type of value | "hello" → string |
%q | Double-quoted string | "hi" → "hi" |
%% | Print a literal % | %% → % |
🧪 Example – Using All Output Functions
package main
import "fmt"
func main() {
name := "Bob"
age := 28
height := 5.9
married := false
fmt.Print("User: ")
fmt.Println(name)
fmt.Printf("%s is %d years old, %.1f feet tall. Married? %t\n", name, age, height, married)
}
📤 Output:
User: Bob
Bob is 28 years old, 5.9 feet tall. Married? false
🧠 Use Cases for Each Output Method
| Function | Use When You… |
|---|---|
fmt.Print() | Need continuous output on the same line |
fmt.Println() | Want simple outputs with automatic spacing & newlines |
fmt.Printf() | Need formatting control for logs, reports, or data tables |
📌 Summary – Recap & Next Steps
Go offers multiple ways to print output, and the fmt package is the primary tool. Whether you’re logging text or formatting data types, Print, Println, and Printf cover your needs.
🔍 Key Takeaways:
fmt.Print()prints without a newlinefmt.Println()prints with a newline and spacingfmt.Printf()formats values using specifiers like%s,%d,%f- Always import
"fmt"to use these functions
⚙️ Next: Explore how to declare and use variables, Go’s type system, and best practices.
❓ FAQs – Go Output
❓ What is the difference between Print() and Println()?
✅ Print() does not add a newline, while Println() automatically adds a newline and spaces between values.
❓ When should I use Printf() in Go?
✅ Use Printf() when you need to format values using placeholders, like in reports, logs, or structured output.
❓ Can I print types and raw values in Go?
✅ Yes. Use %T for the type and %v for the value in Printf().
❓ Do I need any external library to print in Go?
✅ No. The fmt package is part of Go’s standard library and is always available.
❓ Can fmt write to files or just the terminal?
✅ It prints to standard output. To write to files, use the os or bufio packages.
Share Now :
