Go Reference Β· strings and strconv
Go strconv.ParseInt(), ParseFloat() and ParseBool()
Reference for Go strconv.ParseInt, ParseUint, ParseFloat and ParseBool: bases, bit sizes, accepted syntax, range errors and the exact values each one returns.
When Atoi is too limited, the Parse functions in strconv give you control. ParseInt and ParseUint read integers in any base from 2 to 36 and check that the result fits a chosen bit size. ParseFloat reads floating-point numbers, and ParseBool reads the handful of spellings Go accepts for true and false.
All of them return the parsed value and an error, and all report failures as a *strconv.NumError that wraps either strconv.ErrSyntax or strconv.ErrRange.
v, err := strconv.ParseInt("ff", 16, 64) // 255
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func ParseInt(s string, base int, bitSize int) (int64, error) func ParseUint(s string, base int, bitSize int) (uint64, error) func ParseFloat(s string, bitSize int) (float64, error) func ParseBool(str string) (bool, error)
| Name | Description |
|---|---|
s |
The text to parse. No surrounding whitespace is allowed. |
base |
2 to 36, or 0 to infer from a prefix: 0x, 0b, 0o, or a leading 0 for octal; otherwise 10. |
bitSize |
Target size: 0 (means int/uint), 8, 16, 32 or 64. For ParseFloat: 32 or 64. |
Returns: ParseInt an int64 (convert it to a smaller type after checking), ParseUint a uint64 (no sign allowed), ParseFloat a float64, ParseBool a bool.
Errors and edge cases:
- Empty or malformed text gives
ErrSyntaxand the value0. - A value that does not fit
bitSizegivesErrRangewith the nearest limit (for example 127 forint8). - An invalid base or bit size gives a
NumErrorwhose cause is an "invalid base" or "invalid bit size" error, notErrSyntax. ParseFloatreturnsΒ±InfwithErrRangeon overflow, and acceptsinf,infinityandnanin any letter case.ParseBoolaccepts exactly1, t, T, TRUE, true, Trueand0, f, F, FALSE, false, False.
Usage notes
- Choose the bit size to validate input.
ParseInt(s, 10, 32)rejects values that would overflow anint32, so a laterint32(v)conversion is safe. bitSizefor floats is about precision. With 32, the result is afloat64that holds a value already rounded tofloat32.- Underscores are accepted only when
baseis 0, and only between digits or after a prefix. ParseBoolis strict."yes","on"and" true"fail; normalize or write your own mapping for user-facing input.- Locale ignorance.
"1,5"is a syntax error; replace the comma first if you accept European decimals. - JSON numbers are better decoded with
encoding/jsonthan hand-parsed.