HowtoGo
Home / Standard Library / The strconv Package
Standard Library

The strconv Package

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

Integer conversions

FunctionDescription
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

FunctionDescription
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

FunctionDescription
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

FunctionDescription
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

FunctionDescription
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