str "000123" --> int 123
To trim a zero-padded decimal string and convert it to integer as shown above, simply use strconv.Atoi()
function.
package main
import (
"fmt"
"strconv"
)
func ExampleStoi() {
s := "0093"
if i, err := strconv.Atoi(s); err == nil {
fmt.Printf("input=%v, i=%d, type: %T\n", s, i, i)
}
// Output: input=0093, i=93, type: int
}
- View it work online @ Go Playground