strconv converts strings to and from Go's basic types: integers, floats, booleans, and quoted string literals.
Examples
Itoa and Atoi cover the common decimal case. ParseFloat and FormatFloat mirror them for floating-point values, both returning an error alongside the result.
n, err := strconv.Atoi("42")
fmt.Println(n, err)
s := strconv.Itoa(100)
fmt.Println(s)
f, err := strconv.ParseFloat("3.14159", 64)
fmt.Println(f, err)
fmt.Println(strconv.FormatFloat(f, 'f', 2, 64))Output
42
100
3.14159
3.14 ParseInt and FormatInt take an explicit base, so the same pair of functions covers hex, octal, and binary without a separate function per base.
n, err := strconv.ParseInt("2a", 16, 64)
fmt.Println(n, err)
fmt.Println(strconv.FormatInt(255, 16))
fmt.Println(strconv.FormatInt(10, 2))
u, err := strconv.ParseUint("377", 8, 64)
fmt.Println(u, err)Output
42
ff
1010
255 ParseBool accepts several common spellings of true and false. Quote and Unquote round-trip a Go string literal, escapes included.
b, err := strconv.ParseBool("TRUE")
fmt.Println(b, err)
fmt.Println(strconv.FormatBool(false))
q := strconv.Quote("line one\nline two")
fmt.Println(q)
s, err := strconv.Unquote(q)
fmt.Println(s == "line one\nline two", err)Output
true
false
"line one\nline two"
true A failed parse returns a *strconv.NumError, which carries the failing function name and input alongside the underlying error.
_, err := strconv.Atoi("12x")
fmt.Println(err)
var numErr *strconv.NumError
if errors.As(err, &numErr) {
fmt.Println(numErr.Func, numErr.Num)
}Output
strconv.Atoi: parsing "12x": invalid syntax
Atoi 12xInteger conversions
| Function | Description |
|---|---|
Itoa(i int) string strconv.Itoa(42) | Converts i to a decimal string, shorthand for FormatInt(int64(i), 10) |
Atoi(s string) (int, error) strconv.Atoi("42") | Parses s as a decimal integer, shorthand for ParseInt(s, 10, 0) |
ParseInt(s string, base, bitSize int) (int64, error) strconv.ParseInt("2a", 16, 64) | Parses s as a signed integer in the given base and bit size |
ParseUint(s string, base, bitSize int) (uint64, error) strconv.ParseUint("42", 10, 64) | Parses s as an unsigned integer in the given base and bit size |
FormatInt(i int64, base int) string strconv.FormatInt(255, 16) | Formats i as a string in the given base |
FormatUint(i uint64, base int) string strconv.FormatUint(255, 16) | Formats an unsigned integer as a string in the given base |
AppendInt(dst []byte, i int64, base int) []byte strconv.AppendInt(b, 42, 10) | Appends the formatted integer to dst and returns the extended slice |
AppendUint(dst []byte, i uint64, base int) []byte strconv.AppendUint(b, 42, 10) | Appends the formatted unsigned integer to dst |
Float & complex conversions
| Function | Description |
|---|---|
ParseFloat(s string, bitSize int) (float64, error) strconv.ParseFloat("3.14", 64) | Parses s as a floating-point number |
FormatFloat(f float64, fmt byte, prec, bitSize int) string strconv.FormatFloat(3.14159, 'f', 2, 64) | Formats f per the given verb byte and precision |
AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte strconv.AppendFloat(b, 3.14, 'f', 2, 64) | Appends the formatted float to dst |
ParseComplex(s string, bitSize int) (complex128, error) strconv.ParseComplex("1+2i", 128) | Parses s as a complex number |
FormatComplex(c complex128, fmt byte, prec, bitSize int) string strconv.FormatComplex(c, 'f', 2, 128) | Formats a complex number as a string |
Bool conversions
| Function | Description |
|---|---|
ParseBool(s string) (bool, error) strconv.ParseBool("true") | Parses s as a boolean; accepts 1, t, T, TRUE, true, True and the false equivalents |
FormatBool(b bool) string strconv.FormatBool(true) | Formats b as the string "true" or "false" |
AppendBool(dst []byte, b bool) []byte strconv.AppendBool(b, true) | Appends the formatted boolean to dst |
Quoting & unquoting
| Function | Description |
|---|---|
Quote(s string) string strconv.Quote("hi\n") | Returns a double-quoted Go string literal, escaping non-printable characters |
QuoteRune(r rune) string strconv.QuoteRune('é') | Returns a single-quoted Go rune literal |
QuoteToASCII(s string) string strconv.QuoteToASCII("héllo") | Like Quote, but escapes every non-ASCII character |
QuoteRuneToASCII(r rune) string strconv.QuoteRuneToASCII('é') | Like QuoteRune, but escapes a non-ASCII rune |
QuoteToGraphic(s string) string strconv.QuoteToGraphic("café") | Like Quote, but leaves printable Unicode characters unescaped |
QuoteRuneToGraphic(r rune) string strconv.QuoteRuneToGraphic('é') | Like QuoteRune, but leaves a graphic rune unescaped |
AppendQuote(dst []byte, s string) []byte strconv.AppendQuote(b, "hi") | Appends a quoted string to dst |
AppendQuoteRune(dst []byte, r rune) []byte strconv.AppendQuoteRune(b, 'a') | Appends a quoted rune to dst |
AppendQuoteToASCII(dst []byte, s string) []byte strconv.AppendQuoteToASCII(b, "héllo") | Appends an ASCII-escaped quoted string to dst |
AppendQuoteRuneToASCII(dst []byte, r rune) []byte strconv.AppendQuoteRuneToASCII(b, 'é') | Appends an ASCII-escaped quoted rune to dst |
AppendQuoteToGraphic(dst []byte, s string) []byte strconv.AppendQuoteToGraphic(b, "café") | Appends a graphic-preserving quoted string to dst |
AppendQuoteRuneToGraphic(dst []byte, r rune) []byte strconv.AppendQuoteRuneToGraphic(b, 'é') | Appends a graphic-preserving quoted rune to dst |
Unquote(s string) (string, error) strconv.Unquote(`"hi\n"`) | Interprets s as a single-quoted, double-quoted, or backquoted Go string literal |
UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error) strconv.UnquoteChar(`\n`, 0) | Decodes the first character or escape sequence in s |
QuotedPrefix(s string) (string, error) strconv.QuotedPrefix(`"hi" rest`) | Returns the quoted string at the start of s, without unquoting it |
Predicates
| Function | Description |
|---|---|
CanBackquote(s string) bool strconv.CanBackquote("hi") | Reports whether s can be represented unchanged as a backquoted string |
IsPrint(r rune) bool strconv.IsPrint('a') | Reports whether r is defined as printable by Go's rules |
IsGraphic(r rune) bool strconv.IsGraphic('a') | Reports whether r is defined as a Unicode graphic character |