When would you use Strconv Package in a real project
Fast recall
Quick revision
Core answer
Practical understanding of Strconv Package.
Repeating a definition without saying when to use it.
Give one trade-off and say how you would test it.
Interview-ready guide
Interview answer
ParseInt takes (string, base, bitSize). The bitSize bounds what value can come back:
n, err := strconv.ParseInt("255", 10, 8) // fails: 255 > int8 max (127)
m, err := strconv.ParseInt("255", 10, 16) // 255 — fits int16
hex, err := strconv.ParseInt("ff", 16, 32) // 255 — parsed in base 16Executed. The bitSize is a RANGE VALIDATOR — the parse succeeds on well-formed input, then the value is checked against the type's bounds (0 = int/platform, 8 = int8, 16 = int16, 32 = int32, 64 = int64).
Where it matters in real code: validating user input against a column type or protocol field — 'this ID must fit a uint32' becomes ParseInt(s, 10, 32) with the error doing the rejection. It's the parse-and-validate in one call.
The relationship to Atoi: Atoi(s) is exactly ParseInt(s, 10, 0) — decimal, platform int. The shorthand exists because it's 90% of the use; ParseInt is the full control (base + bounds).
The unsigned counterpart: ParseUint with the same shape — and the negative-number asymmetry (ParseUint("-1") fails by definition).
When would you use Strconv Package in a real project
Direct Answer
The bit width of the result (0, 8, 16, 32, 64): ParseInt("ff", 16, 8) fails (255 > int8 max); bitSize 64 succeeds. It bounds the range — a range check before returning.
Deep Dive & Architecture
The mechanics: the parser produces an arbitrary-precision intermediate, then checks it against the bitSize's range (math.MinInt8..MaxInt8 for 8, etc.) before returning — the check is POST-parse, so the error distinguishes 'malformed number' (syntax) from 'valid number, wrong range' (range) by the same error pathway — the error message differs (invalid syntax vs value out of range), which is the detail that lets callers give the right user-facing message.
Why bitSize 0 means 'int': the platform's int (64 on modern systems, 32 on some embedded) — 0 defers to the native width. Code that must be portable across 32-bit targets uses explicit 64 for wide values (parsing a timestamp on a 32-bit platform with bitSize 0 fails for values past 2^31 — the portability bug).
The base parameter's full semantics: 0 means 'infer from prefix' (0x → hex, 0 → octal, else decimal) — matching Go source literals; 1 is invalid; 2-36 explicit. The infer-mode is how ParseInt handles the same strings Go itself parses — the CL flag and config world where "0x1F" and "0755" appear as written. Choosing base 10 explicitly when the input ISN'T Go-source-shaped avoids the surprise of "010" parsing as 8 (octal) rather than 10 — the classic input-validation footgun.
Common Mistakes & Pitfalls
Repeating a definition without saying when to use it.
Check your understanding
Answer these without looking back at the article.
- What is Strconv Package, and when would you use it?
- How would you compare Strconv Package with an alternative?
- What is a common mistake when using Strconv Package?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.