Golang pad string and numbers: fmt padding, leading zeros, and floats

Golang string padding and go padding with fmt.Printf and Sprintf: golang pad string with spaces or zeros, golang format int with leading zeros, golang format float width and precision; optional golang.org/x/text/number.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang pad string and numbers: fmt padding, leading zeros, and floats

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.

go
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")
}
Output

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:

go
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)
}
text
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.

go
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)
}
text
12.346
  12.35
+12.35
0000279.00

golang 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):

go
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")
}
text
|       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:

go
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):

go
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)
}
text
00011

This 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.


References


Frequently Asked Questions

1. How do I pad an integer with leading zeros in Go?

Use a width in the verb, for example fmt.Sprintf("%05d", n) for at least five digits, padding with zeros on the left. The 0 flag selects zero padding for numeric types.

2. How do I pad a string with spaces in Go?

Use a width on %s, for example fmt.Sprintf("%10s", s) for left padding with spaces, or %-10s for right padding. Combine with 0 if you need zero padding on the left for strings.

3. Is there PrintfLn in Go like some other languages?

No. Use fmt.Printf with an explicit newline in the format string, fmt.Println(fmt.Sprintf(...)), or fmt.Println with concatenation.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …