Go Output Functions
In Go, output is handled using functions from the built-in
fmt package. The three most commonly used functions are:
fmt.Print()fmt.Println()fmt.Printf()
Each serves a different purpose depending on how you want to format and display output.
fmt.Print() – Basic Output
The Print() function outputs values using their default format.
It does not automatically add spaces between strings or a newline at the end.
Example:
Adding Line Breaks with \n
To move output to a new line, use the newline escape sequence
\n.
Printing Multiple Values
You can pass multiple arguments to Print().
However, spacing depends on the data types.
Note: When printing non-string values together, Go automatically inserts spaces.
fmt.Println() – Line-Based Output
The Println() function is similar to Print(),
but with two key differences:
- Automatically inserts spaces between arguments
- Adds a newline at the end
fmt.Printf() – Formatted Output
The Printf() function allows you to format output
using format specifiers (verbs). It gives you precise control over
how values are displayed.
Common Format Verbs:
| Verb | Description |
|---|---|
| %v | Value (default format) |
| %T | Type of the value |
When to Use Each Function
- Use
Print()for simple output without formatting -
Use
Println()when you want automatic spacing and line breaks -
Use
Printf()for structured and formatted output
Go Printf() Formatting Verbs
In Go, the fmt.Printf() function provides powerful
formatting capabilities using formatting verbs. These verbs control
how values are displayed in the output, making your programs more
readable and professional.
What Are Formatting Verbs?
Formatting verbs are placeholders (starting with %) used inside a format string. They are replaced by variable values when the program runs.
General Formatting Verbs
| Verb | Description |
|---|---|
| %v | Default value format |
| %#v | Go-syntax representation |
| %T | Type of the value |
| %% | Prints a literal % |
Integer Formatting Verbs
These verbs are used specifically for integer values.
| Verb | Description |
|---|---|
| %b | Binary (base 2) |
| %d | Decimal (base 10) |
| %+d | Decimal with sign |
| %o | Octal |
| %O | Octal with 0o prefix |
| %x | Hexadecimal (lowercase) |
| %X | Hexadecimal (uppercase) |
| %#x | Hex with 0x prefix |
| %4d | Width 4 (right-aligned) |
| %-4d | Width 4 (left-aligned) |
| %04d | Zero-padded width 4 |
String Formatting Verbs
These verbs control how strings are displayed.
| Verb | Description |
|---|---|
| %s | Plain string |
| %q | Double-quoted string |
| %8s | Right-aligned (width 8) |
| %-8s | Left-aligned (width 8) |
| %x | Hex representation |
| % x | Hex with spaces |
Boolean Formatting Verb
| Verb | Description |
|---|---|
| %t | Boolean value (true or false) |
Float Formatting Verbs
These verbs format floating-point numbers.
| Verb | Description |
|---|---|
| %e | Scientific notation |
| %f | Decimal format |
| %.2f | Precision (2 decimal places) |
| %6.2f | Width 6, precision 2 |
| %g | Compact format |
