strconv package.
Parsing Floats
The64 parameter tells how many bits of precision to parse:
Parsing Integers
ForParseInt, the 0 means infer the base from the string. 64 requires that the result fit in 64 bits:
Hexadecimal Numbers
ParseInt recognizes hex-formatted numbers:
Parsing Unsigned Integers
ParseUint is available for unsigned integers:
Convenience Function: Atoi
Atoi is a convenience function for basic base-10 int parsing:
Error Handling
Parse functions return an error on bad input:Parse Functions
Parses a floating-point number. bitSize: 32 or 64
Parses an integer. base: 0 (auto-detect), 2-36. bitSize: 0, 8, 16, 32, 64
Parses an unsigned integer. base: 0 (auto-detect), 2-36. bitSize: 0, 8, 16, 32, 64
Shorthand for ParseInt(s, 10, 0), converted to int
Base Detection
When base is 0,ParseInt auto-detects the base:
0x or 0X prefix - Base 16
0x or 0X prefix - Base 16
Hexadecimal:
0x1c8 → 4560o or 0O prefix - Base 8
0o or 0O prefix - Base 8
Octal:
0o755 → 4930b or 0B prefix - Base 2
0b or 0B prefix - Base 2
Binary:
0b1010 → 10No prefix - Base 10
No prefix - Base 10
Decimal:
123 → 123