Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
From the previous tutorial on how to pad left or print leading zeros with fmt.Printf() function, a reader asked how to pad zeros on right side or print ending zeros to a number.
From https://golang.org/pkg/fmt/ - Other flags:
We can see that there is an option to do just that.... for spaces
-
pad withspaces
on the right rather than the left (left-justify the field)
Therefore, to pad right or print ending space
, all you need to do is to add the -
sign.
However, the trouble is that many people are confused why the -
flag does not pad zero at the right side. If you read between the line, the -
is working as advertise because it only pads spaces
, nothing else. To pad zero(or any other character) to right side, we will need to create our function to do that. See code example below.
Here we go!
package main
import (
"fmt"
"strings"
)
func padRightSide(str string, item string, count int) string {
return str + strings.Repeat(item, count)
}
func main() {
// you want to display 000108 (length = 6)
// pad zero to total length of 6
// d
fmt.Printf("Leading or pad left with zero : |d|\n", 108)
// pad spaces in front(prefix)
fmt.Printf("Leading or pad left with space : |%6d|\n", 108)
// the flag - here WILL NOT work because it only move spaces to the right
// YES only spaces, nothing else...not even zero
fmt.Printf("Ending or pad RIGHT with zero(will NOT work) : |%-06d|\n", 108)
// to pad zero or anything else, we will use the padRightSide function
fmt.Printf("Ending or pad RIGHT with zero : |%v|\n", padRightSide("108", "0", 3))
// pad spaces at end(suffix)
// this will work.
fmt.Printf("Ending or pad RIGHT with space : |%-6d|\n", 108)
}
Output:
Leading or pad left with zero : |000108|
Leading or pad left with space : | 108|
Ending or pad RIGHT with zero(will NOT work) : |108 |
Ending or pad RIGHT with zero : |108000|
Ending or pad RIGHT with space : |108 |
Hope this helps!
References:
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+17.5k Golang : Qt image viewer example
+9k Golang : How to control fmt or log print format?
+31.4k Golang : Convert an image file to []byte
+17.7k Golang : How to log each HTTP request to your web server?
+21.7k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+7.5k Golang : Test if an input is an Armstrong number example
+17.5k Golang : How to make a file read only and set it to writable again?
+10.9k Google Maps URL parameters configuration
+16.5k Golang : read gzipped http response
+6.4k Golang : Skip or discard items of non-interest when iterating example
+14.7k Golang : package is not in GOROOT during compilation
+16.1k Golang : Test floating point numbers not-a-number and infinite example