Most golang string padding and golang pad jobs use the fmt package: fmt.Printf, fmt.Sprintf, and related functions. Width in the format verb controls minimum field size; the 0 flag chooses zero padding for numbers (and can be used with %s for golang pad string with zeros); - left-aligns so extra space appears on the right (golang pad string with spaces on the right). That covers golang printf padding, golang format int with leading zeros, and golang format float precision in one system. For a wider list of verbs, see string interpolation and formatting verbs in Go.
Tested with Go 1.24 on Linux.
golang printf padding: flags and width
A typical verb looks like %[flags][width][.precision]verb. Common flags:
| Flag | Meaning |
|---|---|
- |
Left-align (pad on the right with spaces). |
+ |
Always print a sign for numbers. |
0 |
Pad with zeros instead of spaces when a width is set (for numeric types; with %s, zeros pad on the left). |
The fmt package printing documentation is the authoritative reference.
package main
import "fmt"
func main() {
fmt.Printf("int zero-padded: %05d\n", 7)
fmt.Printf("float width: %8.2f\n", 3.14159)
fmt.Printf("string spaces: |%10s|\n", "hi")
}After Run you should see five-digit style output for the integer, a spaced float field, and the short string padded on the left inside the bars.
golang format int with leading zeros
%d takes a width; with the 0 flag you get golang format int with leading zeros without changing the numeric base:
package main
import "fmt"
func main() {
n := 123
fmt.Printf("spaces: '%5d'\n", n)
fmt.Printf("zeros: '%05d'\n", n)
fmt.Printf("wide: '%05d'\n", 1234567890) // wider than width: no truncation
fmt.Printf("left: '%-5d'\n", n)
}spaces: ' 123'
zeros: '00123'
wide: '1234567890'
left: '123 'To build a padded string without printing, use fmt.Sprintf (same verbs).
golang format float
Width applies to the whole formatted number (including the decimal point). Precision after the dot sets decimal places; values are rounded to fit.
package main
import "fmt"
func main() {
x := 12.3456
fmt.Printf("%7.3f\n", x)
fmt.Printf("%7.2f\n", x)
fmt.Printf("%+.2f\n", x)
fmt.Printf("%010.2f\n", 279.0)
}12.346
12.35
+12.35
0000279.00golang pad string with spaces and golang pad string with zeros
For %s, a width pads with spaces by default; 0 pads on the left with zeros (golang pad string with zeros):
package main
import "fmt"
func main() {
fmt.Printf("|%10s|%10s|\n", "Car", "Brand")
fmt.Printf("|%-10s|%-10s|\n", "Car", "Brand")
fmt.Printf("|%05s|\n", "ab")
}| Car| Brand|
|Car |Brand |
|000ab|For arbitrary pad characters or UTF-8 aware widths, combine strings helpers (for example strings.Repeat) with your own logic.
Printfln and go padding helpers
Go does not define Printfln in the standard library (some GSC hits come from other ecosystems). A one-line helper is enough if you want that shape:
func printfln(format string, a ...any) { fmt.Printf(format+"\n", a...) }Prefer fmt.Printf("...\n", ...) or fmt.Println(fmt.Sprintf(...)) in new code so readers recognize idiomatic go padding examples.
Optional: golang.org/x/text/number
For locale-aware grouping and padding beyond fmt, the golang.org/x/text/number package can build values consumed by message.Printer. It requires adding a module dependency (go get golang.org/x/text):
package main
import (
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/number"
)
func main() {
p := message.NewPrinter(language.English)
n := number.Decimal(11, number.Pad('0'), number.FormatWidth(5))
p.Println(n)
}00011This is optional; most programs stick to fmt for fixed-width columns. For converting floats before integer-style handling, see float64 to int in Go.
Summary
Go padding is usually golang printf padding through fmt verbs: widths and the 0 flag implement golang format int with leading zeros and zero-padded floats; %s with width handles golang pad string with spaces, and %0 width on %s gives golang pad string with zeros. Use fmt.Sprintf when you need a padded string value. There is no built-in Printfln; add a newline explicitly. For heavier localization, consider golang.org/x/text/number behind a module dependency.

