Chapter 12: Output Functions
Output Functions” — the exact topic many beginner tutorials (especially W3Schools-style ones) name as a separate section.
“Output Functions” in Go almost always refers to the three core printing functions from the fmt package:
- fmt.Print()
- fmt.Println()
- fmt.Printf()
These are the first output tools every new Go learner uses after seeing Hello World. They appear in almost every introductory tutorial under headings like:
- “Go Output Functions”
- “Output in Go”
- “Printing in Golang”
- “fmt package basics”
Today I’ll teach them like we’re pair-programming together: differences, when to choose each one, formatting verbs in depth, real patterns, common mistakes, and many examples you can run immediately.
1. Why These Three Functions? (Quick Context)
Go keeps things minimal — no built-in console.log, echo, or System.out.println. Instead, the standard library gives you fmt (format) with exactly these three workhorses for console output (stdout).
All three:
- Take variadic arguments (…any)
- Handle basic types automatically
- Are very fast (used even in production code)
2. Side-by-Side Comparison Table
| Function | Adds newline at end? | Adds space between arguments? | Supports format verbs (%s, %d, etc.)? | Best used when you want… | Typical line count in real code |
|---|---|---|---|---|---|
| fmt.Print() | No | No | No (but can fake it) | Continuous text without breaks | Rare (unless building strings) |
| fmt.Println() | Yes | Yes (one space per arg) | No | Quick debug prints, simple lines | Very high (~60–70% of prints) |
| fmt.Printf() | No (add \n yourself) | No | Yes | Controlled / pretty / aligned output | High (when formatting needed) |
3. Detailed Examples — Run These One by One
Create a file output_functions.go and try each block.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package main import "fmt" func main() { name := "Webliance" age := 25 height := 175.5 city := "Hyderabad" isLearning := true // ──────────────────────────────────────────────── // 1. Print() — raw, no automatic newline or space // ──────────────────────────────────────────────── fmt.Print("Hello") fmt.Print(" from") fmt.Print(" Telangana") // no spaces added automatically fmt.Print("!\n") // you must add \n yourself // Output: // Hello from Telangana! // ──────────────────────────────────────────────── // 2. Println() — friendly, spaces + newline // ──────────────────────────────────────────────── fmt.Println("Name:", name) fmt.Println("Age:", age, "years old") fmt.Println("Height:", height, "cm | City:", city, "| Learning Go:", isLearning) // Output: // Name: Webliance // Age: 25 years old // Height: 175.5 cm | City: Hyderabad | Learning Go: true // Notice: automatic space after ":" and between arguments // ──────────────────────────────────────────────── // 3. Printf() — full control with verbs // ──────────────────────────────────────────────── fmt.Printf("Hello %s from %s! You are %d years old.\n", name, city, age) fmt.Printf("Height: %.1f cm (one decimal)\n", height) fmt.Printf("Learning Go? %t | Type of age: %T\n", isLearning, age) // Output: // Hello Webliance from Hyderabad! You are 25 years old. // Height: 175.5 cm (one decimal) // Learning Go? true | Type of age: int } |
4. Formatting Verbs Cheat Sheet (The Real Power of Printf)
These are the ones you’ll use 95% of the time:
| Verb | Use for | Example Input | Output Example | Notes / Modifiers |
|---|---|---|---|---|
| %v | Default (“value”) | 42, “hi”, true | 42 hi true | Most general — good fallback |
| %s | String | “Webliance” | Webliance | — |
| %d | Integer (decimal) | 25, -42 | 25 -42 | — |
| %f | Float | 175.5 | 175.500000 | Default 6 decimals |
| %.1f | Float — 1 decimal | 175.5 | 175.5 | Change number after . |
| %t | Boolean | true | true | — |
| %T | Type of value | 42 | int | Very useful for debugging |
| %q | Quoted string/rune | “hello”, ‘A’ | “hello” ‘A’ | Good for showing strings safely |
| %x / %X | Hex (lowercase/upper) | 255 | ff FF | Useful for bytes/colors |
| %% | Literal % | — | % | To print % sign |
Alignment & Width examples (very useful for tables):
|
0 1 2 3 4 5 6 7 8 9 10 |
fmt.Printf("%-15s | %4d | %6.1f\n", "Name", "Age", "Height") fmt.Printf("%-15s | %4d | %6.1f\n", name, age, height) // Output: // Name | 25 | 175.5 // (left-aligned name, right-aligned numbers) |
5. Bonus Helpers from fmt (Often Mentioned in “Output Functions” Sections)
- fmt.Sprintf(format, args…) string → formats → returns string (no printing)
|
0 1 2 3 4 5 6 7 |
msg := fmt.Sprintf("User %s is %d years old", name, age) fmt.Println(msg) // or log it, send in HTTP, etc. |
- fmt.Fprintln(os.Stderr, “Critical error!”) → print to stderr (often red in terminals)
6. Common Beginner Mistakes & Tips
- Forgetting \n in Printf → output sticks together on one line
- Using Println when you want no space → use Printf instead
- Too many %v → code becomes hard to read → prefer specific verbs when possible
- No import “fmt” → compile error (most common first-timer issue)
7. Your Quick Practice Task
Try combining all three in one program:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package main import "fmt" func main() { score := 89.7 passed := true fmt.Print("Score: ") fmt.Printf("%.1f ", score) fmt.Println("- Passed?", passed) // Rewrite above using only Printf in one line fmt.Printf("Score: %.1f - Passed? %t\n", score, passed) } |
Which version do you prefer? Why?
Questions now?
- Want all 30+ formatting verbs explained?
- How to make colored output (third-party packages)?
- Output to files / JSON / HTTP responses next?
- Or jump to input functions (Scan, Scanf, etc.)?
Keep running these examples — seeing output is the best feedback loop in programming! 💻🇮🇳 You’re doing great — let’s keep going! 🚀
