Chapter 11: Go Output
Go Output” — how you actually show things to the user (or logs, files, etc.) in Go.
“Go Output” usually means printing / displaying data to the console (standard output = stdout), especially for beginners.
In almost every Go tutorial (W3Schools, freeCodeCamp style, YouTube beginner series, official Tour examples), the section called “Go Output” or “Output Functions” teaches:
- The three main functions from the fmt package: Print()Println()Printf()
These are the first output tools every beginner learns after fmt.Println(“Hello World”).
(There are many other ways to output — files, HTTP responses, JSON, logs — but “Go Output” in beginner context = these three + basic formatting.)
I’ll explain like your teacher sitting next to you: why each exists, differences, when to use which, formatting verbs (%v, %s, %d etc.), common patterns, and tons of runnable examples.
1. The Three Main Output Functions (from import “fmt”)
All live in the fmt package (short for “format”) — import it every time you want output.
| Function | What it does | Adds newline? | Supports formatting verbs? | Most used for? |
|---|---|---|---|---|
| fmt.Print() | Prints arguments as-is, no extra space/newline | No | No (but can use Printf style if you want) | Continuous text without breaks |
| fmt.Println() | Prints arguments + space between + newline at end | Yes | No | Quick debug, simple lines |
| fmt.Printf() | Formatted printing — uses verbs like %s, %d | No (unless you add \n) | Yes | Pretty / controlled output |
2. Basic Examples — Copy-Paste & Run These!
Create output.go:
|
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 |
package main import "fmt" func main() { name := "Webliance" age := 25 height := 175.5 isLearning := true // 1. Print() — no newline, no extra space fmt.Print("Hello ") fmt.Print("from ") fmt.Print("Hyderabad! 🇮🇳\n") // added \n manually // 2. Println() — space between args + newline fmt.Println("Name:", name) // "Name:" + space + value + newline fmt.Println("Age:", age, "years") fmt.Println("Height:", height, "cm | Learning Go:", isLearning) // 3. Printf() — powerful formatting fmt.Printf("Hi %s! You are %d years old.\n", name, age) fmt.Printf("Height: %.1f cm (rounded)\n", height) } |
Run:
|
0 1 2 3 4 5 6 |
go run output.go |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
Hello from Hyderabad! 🇮🇳 Name: Webliance Age: 25 years Height: 175.5 cm | Learning Go: true Hi Webliance! You are 25 years old. Height: 175.5 cm (rounded) |
See the differences?
3. The Real Power: Formatting Verbs in Printf / Sprintf
Printf uses verbs (placeholders) starting with %.
Common ones beginners use first:
| Verb | Meaning | Example value | Output example |
|---|---|---|---|
| %v | Default format (value) | 42, “hi”, true | 42 hi true |
| %s | String | “Webliance” | Webliance |
| %d | Integer (decimal) | 25 | 25 |
| %f | Float | 175.5 | 175.500000 |
| %.1f | Float with 1 decimal | 175.5 | 175.5 |
| %t | Boolean | true | true |
| %T | Type of value | 42 | int |
| %q | Quoted string / char | “hello” | “hello” |
| %x | Hexadecimal | 255 | ff |
| %% | Literal % sign | — | % |
More examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fmt.Printf("Value: %v (default)\n", age) // 25 fmt.Printf("Type: %T\n", height) // float64 fmt.Printf("Bool: %t\n", isLearning) // true fmt.Printf("Quoted: %q\n", "Hyderabad") // "Hyderabad" fmt.Printf("Hex: %x\n", 255) // ff // Multiple args fmt.Printf("%s is %d years old and %.1f cm tall.\n", name, age, height) // → Webliance is 25 years old and 175.5 cm tall. |
4. Other Useful fmt Output Helpers
- fmt.Sprintf() → formats → returns string (no print)
|
0 1 2 3 4 5 6 7 |
msg := fmt.Sprintf("Hello %s from %s!", name, "Telangana") fmt.Println(msg) // or use in logs / HTTP response |
- fmt.Fprintln(os.Stderr, “Error!”) → output to stderr (red in many terminals)
5. Common Patterns Beginners Use
Debug style:
|
0 1 2 3 4 5 6 |
fmt.Printf("DEBUG: user=%s age=%d active=%t\n", name, age, isLearning) |
Table-like output:
|
0 1 2 3 4 5 6 7 8 9 10 |
fmt.Printf("%-15s | %5s | %8s\n", "Name", "Age", "Height") fmt.Printf("%-15s | %5d | %8.1f\n", name, age, height) // Output: // Name | Age | Height // Webliance | 25 | 175.5 |
Error handling classic:
|
0 1 2 3 4 5 6 7 8 9 10 |
file, err := os.Open("data.txt") if err != nil { fmt.Fprintf(os.Stderr, "Error opening file: %v\n", err) return } |
6. Quick Tips & Gotchas (2026 Style)
- Always import “fmt” — no output without it (except panic, log, etc.)
- Println adds space between args — sometimes annoying:
|
0 1 2 3 4 5 6 7 |
fmt.Println("Age:", age) // "Age:" + space + "25" fmt.Printf("Age: %d\n", age) // clean "Age: 25" |
- No automatic JSON pretty-print — use encoding/json + json.MarshalIndent for that.
- For colors in terminal → third-party like github.com/fatih/color (not in stdlib)
- go fmt doesn’t touch strings — your formatting stays safe.
Your Mini Exercise
Write this in output.go and run:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
name := "Webliance" city := "Hyderabad" temp := 32.8 fmt.Println("=== My Info ===") fmt.Printf("Name: %s\n", name) fmt.Printf("City: %-12s Temp: %.1f°C\n", city, temp) fmt.Printf("Type of temp: %T\n", temp) |
Add one more line with %q on a string.
Any confusion?
- Want deeper on all verbs (%p pointers, %b binary, etc.)?
- How to output to files / JSON / HTTP?
- Or next: functions? slices? structs?
You’re doing awesome — keep running code and seeing output! 💻🇮🇳 Let’s go! 🚀
