All packages

strconv

import "strconv"

Package strconv implements conversions to and from string representations of basic data types. # Numeric Conversions The most common numeric conversions are [Atoi] (string to int) and [Itoa] (int to string). i, err := strconv.Atoi("-42") s := strconv.Itoa(-42) These assume decimal and the Go int type. [ParseBool], [ParseFloat], [ParseInt], and [ParseUint] convert strings to values: b, err := strconv.ParseBool("true") f, err := strconv.ParseFloat("3.1415", 64) i, err := strconv.Pars...

Go 1.26.1

Constants

const IntSize = strconv.IntSize

IntSize is the size in bits of an int or uint value.

strconv/number.go:14

Variables

var ErrRange = errors.New("value out of range")

ErrRange indicates that a value is out of range for the target type.

strconv/number.go:246

var ErrSyntax = errors.New("invalid syntax")

ErrSyntax indicates that a value does not have the right syntax for the target type.

strconv/number.go:249

Functions

func AppendBool(dst []byte, b bool) []byte

AppendBool appends "true" or "false", according to the value of b, to dst and returns the extended buffer.

strconv/number.go:34

func AppendFloat(dst []byte, f float64, fmt byte, prec int, bitSize int) []byte

AppendFloat appends the string form of the floating-point number f, as generated by [FormatFloat], to dst and returns the extended buffer.

strconv/number.go:195

func AppendInt(dst []byte, i int64, base int) []byte

AppendInt appends the string form of the integer i, as generated by [FormatInt], to dst and returns the extended buffer.

strconv/number.go:220

func AppendQuote(dst []byte, s string) []byte

AppendQuote appends a double-quoted Go string literal representing s, as generated by [Quote], to dst and returns the extended buffer.

strconv/quote.go:127

func AppendQuoteRune(dst []byte, r rune) []byte

AppendQuoteRune appends a single-quoted Go character literal representing the rune, as generated by [QuoteRune], to dst and returns the extended buffer.

strconv/quote.go:169

func AppendQuoteRuneToASCII(dst []byte, r rune) []byte

AppendQuoteRuneToASCII appends a single-quoted Go character literal representing the rune, as generated by [QuoteRuneToASCII], to dst and returns the extended buffer.

strconv/quote.go:185

func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte

AppendQuoteRuneToGraphic appends a single-quoted Go character literal representing the rune, as generated by [QuoteRuneToGraphic], to dst and returns the extended buffer.

strconv/quote.go:201

func AppendQuoteToASCII(dst []byte, s string) []byte

AppendQuoteToASCII appends a double-quoted Go string literal representing s, as generated by [QuoteToASCII], to dst and returns the extended buffer.

strconv/quote.go:140

func AppendQuoteToGraphic(dst []byte, s string) []byte

AppendQuoteToGraphic appends a double-quoted Go string literal representing s, as generated by [QuoteToGraphic], to dst and returns the extended buffer.

strconv/quote.go:154

func AppendUint(dst []byte, i uint64, base int) []byte

AppendUint appends the string form of the unsigned integer i, as generated by [FormatUint], to dst and returns the extended buffer.

strconv/number.go:226

func Atoi(s string) (int, error)

Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.

strconv/number.go:146

func CanBackquote(s string) bool

CanBackquote reports whether the string s can be represented unchanged as a single-line backquoted string without control characters other than tab.

strconv/quote.go:208

func FormatBool(b bool) string

FormatBool returns "true" or "false" according to the value of b.

strconv/number.go:28

func FormatComplex(c complex128, fmt byte, prec int, bitSize int) string

FormatComplex converts the complex number c to a string of the form (a+bi) where a and b are the real and imaginary parts, formatted according to the format fmt and precision prec. The format fmt and precision prec have the same meaning as in [FormatFloat]. It rounds the result assuming that the original was obtained from a complex value of bitSize bits, which must be 64 for complex64 and 128 for complex128.

strconv/number.go:161

func FormatFloat(f float64, fmt byte, prec int, bitSize int) string

FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64). The format fmt is one of - 'b' (-ddddp±ddd, a binary exponent), - 'e' (-d.dddde±dd, a decimal exponent), - 'E' (-d.ddddE±dd, a decimal exponent), - 'f' (-ddd.dddd, no exponent), - 'g' ('e' for large exponents, 'f' otherwise), - 'G' ...

strconv/number.go:189

func FormatInt(i int64, base int) string

FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' for digit values >= 10.

strconv/number.go:209

func FormatUint(i uint64, base int) string

FormatUint returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' for digit values >= 10.

strconv/number.go:202

func IsGraphic(r rune) bool

IsGraphic reports whether the rune is defined as a Graphic by Unicode. Such characters include letters, marks, numbers, punctuation, symbols, and spaces, from categories L, M, N, P, S, and Zs.

strconv/quote.go:564

func IsPrint(r rune) bool

IsPrint reports whether the rune is defined as printable by Go, with the same definition as [unicode.IsPrint]: letters, numbers, punctuation, symbols and ASCII space.

strconv/quote.go:518

func Itoa(i int) string

Itoa is equivalent to [FormatInt](int64(i), 10).

strconv/number.go:214

func ParseBool(str string) (bool, error)

ParseBool returns the boolean value represented by the string. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error.

strconv/number.go:19

func ParseComplex(s string, bitSize int) (complex128, error)

ParseComplex converts the string s to a complex number with the precision specified by bitSize: 64 for complex64, or 128 for complex128. When bitSize=64, the result still has type complex128, but it will be convertible to complex64 without changing its value. The number represented by s must be of the form N, Ni, or N±Ni, where N stands for a floating-point number as recognized by [ParseFloat], and i is the imaginary component. If the second N is unsigned, a + sign is required between the t...

strconv/number.go:58

func ParseFloat(s string, bitSize int) (float64, error)

ParseFloat converts the string s to a floating-point number with the precision specified by bitSize: 32 for float32, or 64 for float64. When bitSize=32, the result still has type float64, but it will be convertible to float32 without changing its value. ParseFloat accepts decimal and hexadecimal floating-point numbers as defined by the Go syntax for [floating-point literals]. If s is well-formed and near a valid floating-point number, ParseFloat returns the nearest floating-point number roun...

strconv/number.go:93

func ParseInt(s string, base int, bitSize int) (int64, error)

ParseInt interprets a string s in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value i. The string may begin with a leading sign: "+" or "-". If the base argument is 0, the true base is implied by the string's prefix following the sign (if present): 2 for "0b", 8 for "0" or "0o", 16 for "0x", and 10 otherwise. Also, for argument base 0 only, underscore characters are permitted as defined by the Go syntax for [integer literals]. The bitSize argument speci...

strconv/number.go:137

func ParseUint(s string, base int, bitSize int) (uint64, error)

ParseUint is like [ParseInt] but for unsigned numbers. A sign prefix is not permitted.

strconv/number.go:104

func Quote(s string) string

Quote returns a double-quoted Go string literal representing s. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for control characters and non-printable characters as defined by [IsPrint].

strconv/quote.go:121

func QuotedPrefix(s string) (string, error)

QuotedPrefix returns the quoted string (as understood by [Unquote]) at the prefix of s. If s does not start with a valid quoted string, QuotedPrefix returns an error.

strconv/quote.go:368

func QuoteRune(r rune) string

QuoteRune returns a single-quoted Go character literal representing the rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for control characters and non-printable characters as defined by [IsPrint]. If r is not a valid Unicode code point, it is interpreted as the Unicode replacement character U+FFFD.

strconv/quote.go:163

func QuoteRuneToASCII(r rune) string

QuoteRuneToASCII returns a single-quoted Go character literal representing the rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for non-ASCII characters and non-printable characters as defined by [IsPrint]. If r is not a valid Unicode code point, it is interpreted as the Unicode replacement character U+FFFD.

strconv/quote.go:179

func QuoteRuneToGraphic(r rune) string

QuoteRuneToGraphic returns a single-quoted Go character literal representing the rune. If the rune is not a Unicode graphic character, as defined by [IsGraphic], the returned string will use a Go escape sequence (\t, \n, \xFF, \u0100). If r is not a valid Unicode code point, it is interpreted as the Unicode replacement character U+FFFD.

strconv/quote.go:195

func QuoteToASCII(s string) string

QuoteToASCII returns a double-quoted Go string literal representing s. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for non-ASCII characters and non-printable characters as defined by [IsPrint].

strconv/quote.go:134

func QuoteToGraphic(s string) string

QuoteToGraphic returns a double-quoted Go string literal representing s. The returned string leaves Unicode graphic characters, as defined by [IsGraphic], unchanged and uses Go escape sequences (\t, \n, \xFF, \u0100) for non-graphic characters.

strconv/quote.go:148

func Unquote(s string) (string, error)

Unquote interprets s as a single-quoted, double-quoted, or backquoted Go string literal, returning the string value that s quotes. (If s is single-quoted, it would be a Go character literal; Unquote returns the corresponding one-character string. For an empty character literal Unquote returns the empty string.)

strconv/quote.go:379

func UnquoteChar(s string, quote byte) (rune, bool, string, error)

UnquoteChar decodes the first character or byte in the escaped string or character literal represented by the string s. It returns four values: 1. value, the decoded Unicode code point or byte value; 2. multibyte, a boolean indicating whether the decoded character requires a multibyte UTF-8 representation; 3. tail, the remainder of the string after the character; and 4. an error that will be nil if the character is syntactically valid. The second argument, quote, specifies the type of l...

strconv/quote.go:255

Types

type NumError struct {
	Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat, ParseComplex)
	Num  string // the input
	Err  error  // the reason the conversion failed (e....

A NumError records a failed conversion.

strconv/number.go:252

func (e *NumError) Error() string

strconv/number.go:258

func (e *NumError) Unwrap() error

strconv/number.go:262