How to pad a number with zeros when printing?

The fmt package can do this for you:

fmt.Printf("|%06d|%6d|\n", 12, 345)


|000012|   345|

Notice the 0 in %06d, that will make it a width of 6 and pad it with zeros. The second one will pad with spaces.

Try it for yourself here:

Leave a Comment