Handling Numeric Input in Go: Base Interpretation Mistakes to Avoid
The problem: unexpected behaviour with leading zeros
While working on a Go program that reverses an integer input, I hit an unexpected issue: values with leading zeros produced incorrect results. Entering 0123 returned 83 instead of 321. After some debugging, I realised the cause was rooted in Go’s interpretation of numeric literals.
package main
import "fmt"
func main() {
var inp int
fmt.Scan(&inp)
fmt.Println(inp) // Outputs unexpected values for leading-zero inputs
}
Why would Go interpret 0123 as 83? Let’s look at the mechanics behind Go’s input handling.
Numeric literals in Go
In Go, numeric literals are interpreted based on their prefixes:
- Decimal — no prefix (e.g.
123). - Octal — numbers starting with
0, like0123, are base 8. - Hexadecimal — prefixed with
0xor0X, like0x1F. - Binary — prefixed with
0bor0B, like0b1010. Introduced in Go 1.13.
Because 0123 has a 0 prefix, Go treats it as octal, converting it to 83 in decimal — which explains the unexpected output.
The fix: parse explicitly
If you’re dealing with user input that might include leading zeros and you want decimal interpretation, read the input as a string and convert it explicitly:
package main
import (
"fmt"
"strconv"
)
func main() {
var inp string
fmt.Scan(&inp)
num, err := strconv.Atoi(inp)
if err != nil {
fmt.Println("Invalid input")
return
}
fmt.Println(num) // Decimal, regardless of leading zeros
}
Other situations to watch for
strconv.ParseIntwith base 0 —strconv.ParseInt("0123", 0, 64)detects the base from the prefix. If you need consistent decimal interpretation, pass10instead.- Hex and binary literals in code — useful, but make sure they aren’t used inadvertently:
0xand0bprefixes trigger hexadecimal and binary parsing.
Key takeaways
- Read input as strings when the data could carry ambiguous prefixes, and specify the base during conversion.
- Explicitly specify the base when parsing with
strconvto avoid automatic base detection. - Watch out for leading zeros in integer input, where the base might unintentionally switch to octal.
Understanding these nuances prevents bugs and keeps numeric input predictable.