How padding works in Golang?
We can use golang fmt.Printf in-built function to perform golang
formatting. To format a string we will use %s while to format an
integer we will use %d. You can read more these verbs atgolang
formatting verbs
andfmt.Printf Documentation.
Following table explains the different Formatting Verb Modifiers
| Modifier | Description |
|---|---|
| + | This modifier (the plus sign) always prints a sign, positive or negative, for numeric values. |
| 0 | This modifier uses zeros, rather than spaces, aspaddingwhen the width is greater than the number of characters required to display the value. |
| - | This modifier (the subtracts symbol) addspaddingto the right of the number, rather than the left. |
Example-1: Add padding in float numbers
In this examples we will use a float number and add some padded text towards left and right side of the float number:
package main
import "fmt"
func Printfln(template string, values ...interface{}) {
fmt.Printf(template+"\n", values...)
}
func main() {
number := 279.00
Printfln("Sign: >>%+.2f<<", number)
Printfln("Zeros for Padding: >>%010.2f<<", number)
Printfln("Right Padding: >>%-8.2f<<", number)
Printfln("Left Padding: >>%8.2f<<", number)
}
Output:
Sign: >>+279.00<<
Zeros for Padding: >>0000279.00<<
Right Padding: >>279.00 <<
Left Padding: >> 279.00<<
Example-2: Formatting fractional number widths for float numbers
The minimum width of the entire number includes decimal places and the decimal point. If it’s included, shorter numbers will be padded with spaces at the start until this width is reached. If it’s omitted, no spaces will ever be added.
The width after the decimal point is the number of decimal places to show. If a more precise number is given, it will be rounded (up or down) to fit in the given number of decimal places.
package main
import "fmt"
func main() {
number := 12.3456
fmt.Printf("%%7.3f: %7.3f\n", number) // Rounded to three places
fmt.Printf("%%7.2f: %7.2f\n", number) // Rounded to two places
fmt.Printf("%%7.1f: %7.1f\n", number) // Rounded to one place
fmt.Printf("%%.1f: %.1f\n", number) // Rounded to one place, no padding
fmt.Printf("%%.2f: %.2f\n", number) // Rounded to one place, no padding
}
Output:
%7.3f: 12.346
%7.2f: 12.35
%7.1f: 12.3
%.1f: 12.3
%.2f: 12.35
Example-3: Formatting integer number widths
Thefmtpackage provides several ways to pad integers when formatting.
The%dverb can be modified to add padding to the right or left of the
integer. In either case, this padding is considered aminimumpadding.
If a number is larger than the minimum padding, the number is printed as
it is.
package main
import "fmt"
func main() {
d := 123
fmt.Printf("Padded: '%5d'\n", d) // left padding with 5 space character
fmt.Printf("Padded: '%05d'\n", d) // left padding with 5 '0' character
fmt.Printf("Padded: '%5d'\n", 1234567890) // number larger than padded value
fmt.Printf("Padded: '%-5d'\n", d) // print integer padded on the right
fmt.Printf("Padded: '%-5d'\n", 1234567890) // number larger than padded value
}
Output
Padded: ' 123'
Padded: '00123'
Padded: '1234567890'
Padded: '123 '
Padded: '1234567890'
Example-4: Golang string padding in the left
Here is an example to perform golang string padding on the left
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Printf("+%s+\n", strings.Repeat("-", 25))
fmt.Printf("| %10s | %10s |\n", "Car", "Brand")
fmt.Printf("|%s|\n", strings.Repeat("-", 25))
fmt.Printf("| %10s | %10s |\n", "Baleno", "Nexa")
fmt.Printf("+%s+\n", strings.Repeat("-", 25))
fmt.Printf("| %10s | %10s |\n", "Nexon", "Tata")
fmt.Printf("+%s+\n", strings.Repeat("-", 25))
}
Output:
+-------------------------+
| Car | Brand |
|-------------------------|
| Baleno | Nexa |
+-------------------------+
| Nexon | Tata |
+-------------------------+
Example-5: Golang string padding in the right
Here is another example to perform string padding towards the right side:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Printf("+%s+\n", strings.Repeat("-", 25))
fmt.Printf("| %-10s | %-10s |\n", "Car", "Brand")
fmt.Printf("|%s|\n", strings.Repeat("-", 25))
fmt.Printf("| %-10s | %-10s |\n", "Baleno", "Nexa")
fmt.Printf("+%s+\n", strings.Repeat("-", 25))
fmt.Printf("| %-10s | %-10s |\n", "Nexon", "Tata")
fmt.Printf("+%s+\n", strings.Repeat("-", 25))
}
Output:
+-------------------------+
| Car | Brand |
|-------------------------|
| Baleno | Nexa |
+-------------------------+
| Nexon | Tata |
+-------------------------+
Example-6: Using golang.org/x/text/number package to pad an int
We can use the text/number package to pad an int. Here is an example of how to do that, here the pad character is 0, we can pad with any other characters if required:
import (
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/number"
)
var fmt = message.NewPrinter(language.English)
func main() {
num := number.Decimal(
11, number.Pad('0'), number.FormatWidth(5),
)
fmt.Println(num) // 00011
}
Output:
00011
Example-7: Return int as string after padding
Use fmt.Sprintf instead of printing to the standard output if you want
to return a string for later use:
package main
import "fmt"
func main() {
var intSlice = []int{2, 20, 200, 236, 1104, 25363}
for _, v := range intSlice {
// return a string instead of printing it
str := fmt.Sprintf("%05d", v)
fmt.Println(str)
fmt.Println("----")
}
}
Output:
00002
----
00020
----
00200
----
00236
----
01104
----
25363
----
Summary
With the help of the fmt.Printf() and %d verb, we can easily pad 0
or space to an int or a string. The verb %05d means the fixed length
is 5 and the pad character is 0 while %5d means the fixed length is 5
and the pad character is the space character. We also can pad space to
the right of a string or an int by using (-) signal before the verb
(%-5d). For padding int, we have a more powerful package text/number
that formats numbers according to the customs of different locales. If
you want to return the padded string, use the fmt.Sprintf function
instead of the fmt.Printf.
References
How
to pad a number with zeros when printing? - Stack Overflow
Usage
example for Format method with leading zero padding
Golang fmt
printf right padding - Stack Overflow

![Golang Padding String, Int or Float [SOLVED]](/golang-padding-formatting/golang-padding.jpg)
