Golang fmt.Printf function
last modified May 8, 2025
This tutorial explains how to use the fmt.Printf
function in Go.
We'll cover formatting basics with practical examples of formatted output.
The fmt.Printf function formats according to a format specifier and writes to standard output. It returns the number of bytes written and any error. This is Go's primary function for formatted output.
In Go, fmt.Printf
uses format verbs that begin with % to specify
how values are displayed. These verbs control padding, precision, and data type
representation in the output.
Basic Printf example
The simplest use of fmt.Printf
displays variables with default
formatting. This example demonstrates basic string and number formatting.
Note: The format string must match the number of arguments.
package main import "fmt" func main() { name := "Alice" age := 28 height := 1.68 fmt.Printf("Name: %s, Age: %d, Height: %.2f\n", name, age, height) }
The format string contains three verbs: %s for string, %d for integer, and %.2f for float with 2 decimal places. The values are inserted in order.
Formatting integers
Integer formatting offers various options for base representation and padding. This example shows different integer formatting techniques.
package main import "fmt" func main() { num := 42 fmt.Printf("Decimal: %d\n", num) fmt.Printf("Binary: %b\n", num) fmt.Printf("Hexadecimal: %x\n", num) fmt.Printf("Octal: %o\n", num) fmt.Printf("With sign: %+d\n", num) fmt.Printf("Padded: %6d\n", num) fmt.Printf("Leading zeros: %06d\n", num) }
Each verb displays the number differently. %b shows binary, %x hexadecimal, and %o octal. Padding adds spaces or zeros to reach the specified width.
Floating-point formatting
Floating-point numbers can be formatted with precision, scientific notation, and automatic formatting. This example demonstrates float formatting options.
package main import "fmt" func main() { pi := 3.1415926535 fmt.Printf("Default: %f\n", pi) fmt.Printf("Precision 2: %.2f\n", pi) fmt.Printf("Precision 4: %.4f\n", pi) fmt.Printf("Scientific: %e\n", pi) fmt.Printf("Large exponent: %g\n", 123456789.123456789) fmt.Printf("Width 10, precision 3: %10.3f\n", pi) }
The precision controls decimal places. %e uses scientific notation, while %g automatically chooses between %f and %e based on the exponent size.
String formatting
String formatting includes padding, truncation, and quoting options. This example shows various string formatting techniques.
package main import "fmt" func main() { str := "Hello, 世界" fmt.Printf("Default: %s\n", str) fmt.Printf("Quoted: %q\n", str) fmt.Printf("Hex: % x\n", str) fmt.Printf("Right-padded: %-20s!\n", str) fmt.Printf("Left-padded: %20s!\n", str) fmt.Printf("Truncated: %.5s\n", str) }
%q adds quotes, %x shows hex bytes, and %.5s truncates after 5 bytes (not runes). Padding aligns text left or right within the specified width.
Boolean and pointer formatting
Boolean values and pointers have specific formatting verbs. This example demonstrates how to display these types.
package main import "fmt" func main() { flag := true ptr := &flag fmt.Printf("Boolean: %t\n", flag) fmt.Printf("Pointer: %p\n", ptr) fmt.Printf("Pointer value: %v\n", ptr) fmt.Printf("Type and value: %T %v\n", ptr, ptr) }
%t formats booleans as "true" or "false". %p shows pointer addresses in hex. %T displays the type of a value, useful for debugging.
Struct formatting
Structs can be formatted with different verbs to control output detail. This example shows struct formatting options.
package main import "fmt" type Person struct { Name string Age int } func main() { p := Person{"Bob", 42} fmt.Printf("Default: %v\n", p) fmt.Printf("With field names: %+v\n", p) fmt.Printf("Go syntax: %#v\n", p) fmt.Printf("Type: %T\n", p) }
%v shows values, %+v includes field names, and %#v displays Go syntax that would recreate the value. These are useful for debugging complex types.
Custom formatting with Stringer
Types can implement the Stringer interface to customize their formatting. This example demonstrates custom formatting for a user-defined type.
package main import "fmt" type Point struct { X, Y int } func (p Point) String() string { return fmt.Sprintf("(%d,%d)", p.X, p.Y) } func main() { p := Point{3, 4} fmt.Printf("Point: %v\n", p) fmt.Printf("String: %s\n", p) fmt.Printf("Go syntax: %#v\n", p) }
By implementing String(), Point controls how it appears in formatted output. This is called automatically by %v and %s format verbs.
Source
This tutorial covered the fmt.Printf
function in Go with practical
examples of formatted output and various formatting techniques.
Author
List all Golang tutorials.