Golang: How to pad a number with zeros when printing?

How can I print a number or make a string with zero padding to make it fixed width?

For instance, if I have the number 12 and I want to make it 000012 .

The fmt package can do this for you:

fmt.Printf("|%06d|%6d|\n", 12, 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.

You can see it in action here: http://play.golang.org/p/cinDspMccp

Refer: stackoverflow.com