Printing and Formatting in Go
Printing and Formatting in Go
Formatting output is one of Go’s most practical tools — especially when you’re
printing totals, diagnostics, or structured values. Go’s
Printf
function gives you precise control over numbers, alignment, and readability.
Source Code
fmt.Printf("total: %d items (avg: %.2f)\n", count, average)
Pseudocode
# import fmt
# call Printf with formatting verbs
# %d formats an integer
# %.2f formats a float with two decimal places
# \n adds a newline at the end
-
Formatting verbs
—
%dprints integers,%.2fprints floats with fixed precision. -
Precision control
—
%.2fensures consistent decimal formatting. -
Newline behavior
—
Printfdoes not add a newline unless you include\n. - Structured output — useful for logs, summaries, and metrics.
Expected Output
total: 12 items (avg: 3.75)
Formatted printing is a core Go pattern: once you understand how verbs work, you can cleanly print slices, structs, diagnostics, and aligned tables without extra libraries.
