Output
Two Families of Printing in Go
-
print()andprintln()→ low-level, built-in functions (from the Go runtime, not fmt package). -
fmt.Printf()and friends → from thefmtpackage, much more powerful and commonly used
print()
- Purpose: Print text or values in a basic way.
- No formatting: It just writes values as-is.
- No automatic newline: You control spacing manually.
- Arguments: Can take multiple arguments, prints them directly.
- Not recommended for production code (meant for quick debugging).
println()
- Same as
print(), but automatically adds a newline (\n) at the end. - Still no fancy formatting, just prints each argument separated by a space.
With multiple arguments:
println("Score:", 100) // Score: 100 - Notice the space between arguments.
fmt.Printf()
- Comes from the fmt package.
- Formatted printing — you can control how values are shown.
- Requires format verbs (placeholders like %d, %s, %f) inside a string.
- No automatic newline — you must add \n if you want one.
Common verbs:
%d→ integer%s→ string%f→ floating point%t→ boolean%v→ value (automatic format)%T→ type of value
fmt.Printf("Name: %s, Age: %d\n", "Alice", 25) // Name: Alice, Age: 25
Comparison of Outputs
| Feature | print() | println() | fmt.Printf() |
|---|---|---|---|
| Package needed | No (built-in) | No (built-in) | Yes (import "fmt") |
| Auto newline | No | Yes | No (unless \n added) |
| Formatting control | No | No | Yes (format verbs) |
| Multiple arguments | Yes (no format) | Yes (space-separated) | Yes (matches placeholders) |
| Recommended for real apps | ❌ Debug only | ❌ Debug only | ✅ Yes |