It is sometimes necessary to pad a number with zeros to ensure a minumum width.
For example, if you want a width of at least four digits the number 9 becomes
“0009”, the number 10 becomes “0010”, and so on. The
fmt package can do this
The presence of
%04d is what causes the value to be printed with a width of
4 and the
0 as the padding character. If the provided value has four or more
digits, the value will be printed as is without being padded.
If you don’t want to return a string for later use instead of printing to the
standard output, use
s := fmt.Sprintf("%04d", 45) fmt.Println(s) // 0045
Thanks for reading, and happy coding!