Zero padding in Golang

Zero padding in Golang

For 0-9, add 0 before the number If it is 10 or more, it can be realized as follows if 0 is not added.

s := 3
str := fmt.Sprintf("%02d", s)
fmt.Println(str)
// 03

s2 := 13
str2 := fmt.Sprintf("%02d", s2)
fmt.Println(str2)
// 13

​ I realized it in the following roundabout way without knowing it, so it was a shock when I knew it ...

if num < 10 {
​
    numStr = "0" + strconv.Itoa(num)
​
} else {
​
    numStr = strconv.Itoa(num)
​
}

Recommended Posts

Zero padding in Golang
Clear CloudFront cache in Golang
Install GoLang in goenv of anyenv
Double quote csv elements in Golang
How to dynamically zero pad in Python
Control files to build in Golang project