Go Output Functions
Go's standard library provides output through the fmt package. Three functions cover almost every case you'll need:
fmt.Print()— prints values with minimal formatting.fmt.Println()— prints values with automatic spacing and a trailing newline.fmt.Printf()— prints values using a format string with placeholders, for precise control over output.
fmt.Print() — Basic Output
Print() prints its arguments using their default format. It adds a space between two operands only when neither is a string, and it never adds a trailing newline.
package main
import "fmt"
func main() {
message1 := "Good"
message2 := "Morning"
fmt.Print(message1)
fmt.Print(message2)
}
Expected output:
GoodMorning
Adding Line Breaks with \n
To force a new line, include the newline escape sequence \n inside the string:
package main
import "fmt"
func main() {
fmt.Print("Welcome\n")
fmt.Print("To Go Programming\n")
}
Expected output:
Welcome
To Go Programming
Printing Multiple Values with Print()
package main
import "fmt"
func main() {
name := "Anita"
age := 22
fmt.Print("Name: ", name, "\nAge: ", age)
}
Expected output:
Name: Anita
Age: 22
Note on spacing: Print()'s rule is easy to misremember — it adds a space between two adjacent operands only when neither is a string. In the example above, "Name: " and name are both strings, so no extra space is inserted between them (which is why the string already ends with a space); but between "\nAge: " and age (a string and an int), a space is added automatically. This inconsistency is exactly why Println() is preferred for most everyday printing — it removes the guesswork.
fmt.Println() — Line-Based Output
Println() behaves like Print(), but with two differences that make it the default choice for simple output:
- It always inserts a space between arguments, regardless of type.
- It always adds a newline at the end.
package main
import "fmt"
func main() {
city := "Chennai"
temperature := 32
fmt.Println("City:", city, "Temperature:", temperature)
}
Expected output:
City: Chennai Temperature: 32
fmt.Printf() — Formatted Output
Printf() takes a format string containing verbs — placeholders starting with % — and substitutes each one with the corresponding argument, in order. Unlike Print and Println, Printf() does not add a trailing newline automatically; you must include \n yourself.
package main
import "fmt"
func main() {
language := "Go"
year := 2009
fmt.Printf("%s was released in %d\n", language, year)
}
Expected output:
Go was released in 2009
When to Use Each Function
- Use
Print()for quick, minimally formatted output — rare in practice because of its inconsistent spacing rule. - Use
Println()for simple output where you just want values separated by spaces and ending in a newline. - Use
Printf()whenever you need control over formatting — padding, decimal precision, type inspection, or building structured log lines.
Printf() Formatting Verbs
Formatting verbs are the placeholders inside a Printf() format string. Each verb expects a matching argument of a compatible type; a mismatch (say, using %d with a string) produces a visible error marker in the output rather than a silent failure, which is one of Go's safer design choices.
General-Purpose Verbs
| Verb | Description |
|---|---|
%v | Default format for the value |
%#v | Go-syntax representation of the value |
%T | The value's type |
%% | A literal % character |
package main
import "fmt"
func main() {
value := 42.75
text := "GoLang"
fmt.Printf("Value: %v\n", value)
fmt.Printf("Go-syntax: %#v\n", value)
fmt.Printf("Type: %T\n", value)
fmt.Printf("Completion: %v%%\n", 85)
fmt.Printf("Text: %v | %#v | %T\n", text, text, text)
}
Integer Verbs
| Verb | Description |
|---|---|
%b | Binary (base 2) |
%d | Decimal (base 10) |
%+d | Decimal, always showing the sign |
%o | Octal |
%O | Octal, with a 0o prefix |
%x / %X | Hexadecimal (lowercase / uppercase) |
%#x | Hexadecimal with a 0x prefix |
%4d | Minimum width 4, right-aligned |
%-4d | Minimum width 4, left-aligned |
%04d | Minimum width 4, zero-padded |
package main
import "fmt"
func main() {
number := 27
fmt.Printf("Binary: %b\n", number)
fmt.Printf("Decimal: %d\n", number)
fmt.Printf("Signed: %+d\n", number)
fmt.Printf("Octal: %o\n", number)
fmt.Printf("Octal (prefixed): %O\n", number)
fmt.Printf("Hex (lower): %x\n", number)
fmt.Printf("Hex (upper): %X\n", number)
fmt.Printf("Hex (with prefix): %#x\n", number)
fmt.Printf("Right padded: %4d\n", number)
fmt.Printf("Left padded: %-4d\n", number)
fmt.Printf("Zero padded: %04d\n", number)
}
String Verbs
| Verb | Description |
|---|---|
%s | Plain string |
%q | Double-quoted, escaped string |
%8s | Right-aligned, minimum width 8 |
%-8s | Left-aligned, minimum width 8 |
%x | Hex-encoded bytes of the string |
% x | Hex-encoded bytes, space-separated |
package main
import "fmt"
func main() {
word := "Code"
fmt.Printf("Plain: %s\n", word)
fmt.Printf("Quoted: %q\n", word)
fmt.Printf("Right aligned: %8s\n", word)
fmt.Printf("Left aligned: %-8s\n", word)
fmt.Printf("Hex: %x\n", word)
fmt.Printf("Hex (spaced): % x\n", word)
}
Boolean Verb
| Verb | Description |
|---|---|
%t | true or false |
package main
import "fmt"
func main() {
isLoggedIn := true
isAdmin := false
fmt.Printf("Logged in: %t\n", isLoggedIn)
fmt.Printf("Admin: %t\n", isAdmin)
}
Floating-Point Verbs
| Verb | Description |
|---|---|
%e | Scientific notation |
%f | Standard decimal notation |
%.2f | Fixed to 2 decimal places |
%6.2f | Minimum width 6, 2 decimal places |
%g | Compact format — %e for very large/small numbers, %f otherwise |
package main
import "fmt"
func main() {
pi := 3.14159
fmt.Printf("Scientific: %e\n", pi)
fmt.Printf("Decimal: %f\n", pi)
fmt.Printf("Rounded: %.2f\n", pi)
fmt.Printf("Width + precision: %6.2f\n", pi)
fmt.Printf("Compact: %g\n", pi)
}
Expected output:
Scientific: 3.141590e+00
Decimal: 3.141590
Rounded: 3.14
Width + precision: 3.14
Compact: 3.14159
Common mistake: forgetting that %d is for integers only — passing a float64 to %d produces an error marker like %!d(float64=3.14) in the output instead of a number. Use %f, %g, or %v for floating-point values.