strconv

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package strconv implements conversions to and from string representations of basic data types.

The most common numeric conversions are Atoi (string to int) and Itoa (int to string).

i, err := strconv.Atoi("-42")
buf := make([]byte, 64)
s := strconv.Itoa(buf, -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.ParseInt("-42", 10, 64)
u, err := strconv.ParseUint("42", 10, 64)

The parse functions return the widest type (float64, int64, and uint64), but if the size argument specifies a narrower width the result can be converted to that narrower type without data loss:

s := "2147483647" // biggest int32
i64, err := strconv.ParseInt(s, 10, 32)
...
i := int32(i64)

FormatBool, FormatFloat, FormatInt, and FormatUint convert values to strings:

buf := make([]byte, 64)
s := strconv.FormatBool(true)
s := strconv.FormatFloat(buf, 3.1415, 'E', -1, 64)
s := strconv.FormatInt(buf, -42, 16)
s := strconv.FormatUint(buf, 42, 16)

AppendBool, AppendFloat, AppendInt, and AppendUint are similar but append the formatted value to a destination slice.

Quote/Unquote conversions are not supported.

Based on the strconv package.

Code generated by: go run pow10gen.go. DO NOT EDIT.

Index

Examples

Constants

View Source
const IntSize = intSize

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

Variables

View Source
var ErrBase = errors.New("invalid base")
View Source
var ErrBitSize = errors.New("invalid bit size")
View Source
var ErrRange = errors.New("value out of range")
View Source
var ErrSyntax = errors.New("invalid syntax")
View Source
var ErrUnknown = errors.New("unknown error")

Functions

func AppendBool

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.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	b := []byte("bool:")
	b = strconv.AppendBool(b, true)
	fmt.Println(string(b))

}
Output:
bool:true

func AppendFloat

func AppendFloat(dst []byte, f float64, fmt byte, prec, 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.

dst free capacity must be at least prec+4 bytes when prec >= 0, and at least 24 bytes when prec < 0.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	b32 := []byte("float32:")
	b32 = strconv.AppendFloat(b32, 3.1415926535, 'E', -1, 32)
	fmt.Println(string(b32))

	b64 := []byte("float64:")
	b64 = strconv.AppendFloat(b64, 3.1415926535, 'E', -1, 64)
	fmt.Println(string(b64))

}
Output:
float32:3.1415927E+00
float64:3.1415926535E+00

func AppendInt

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. dst must have enough capacity to hold the result.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	b10 := []byte("int (base 10):")
	b10 = strconv.AppendInt(b10, -42, 10)
	fmt.Println(string(b10))

	b16 := []byte("int (base 16):")
	b16 = strconv.AppendInt(b16, -42, 16)
	fmt.Println(string(b16))

}
Output:
int (base 10):-42
int (base 16):-2a

func AppendUint

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. dst must have enough capacity to hold the result.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	b10 := []byte("uint (base 10):")
	b10 = strconv.AppendUint(b10, 42, 10)
	fmt.Println(string(b10))

	b16 := []byte("uint (base 16):")
	b16 = strconv.AppendUint(b16, 42, 16)
	fmt.Println(string(b16))

}
Output:
uint (base 10):42
uint (base 16):2a

func Atoi

func Atoi(s string) (int, error)

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

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	v := "10"
	if s, err := strconv.Atoi(v); err == nil {
		fmt.Printf("%T, %v", s, s)
	}

}
Output:
int, 10

func FormatBool

func FormatBool(b bool) string

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

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	v := true
	s := strconv.FormatBool(v)
	fmt.Printf("%T, %v\n", s, s)

}
Output:
string, true

func FormatFloat

func FormatFloat(dst []byte, f float64, fmt byte, prec, 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' ('E' for large exponents, 'f' otherwise),
  • 'x' (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or
  • 'X' (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent).

The precision prec controls the number of digits (excluding the exponent) printed by the 'e', 'E', 'f', 'g', 'G', 'x', and 'X' formats. For 'e', 'E', 'f', 'x', and 'X', it is the number of digits after the decimal point. For 'g' and 'G' it is the maximum number of significant digits (trailing zeros are removed). The special precision -1 uses the smallest number of digits necessary such that ParseFloat will return f exactly. The exponent is written as a decimal integer; for all formats other than 'b', it will be at least two digits.

dst length must be at least prec+4 bytes when prec >= 0, and at least 24 bytes when prec < 0.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	v := 3.1415926535
	buf := make([]byte, 64)

	s32 := strconv.FormatFloat(buf, v, 'E', -1, 32)
	fmt.Printf("%T, %v\n", s32, s32)

	s64 := strconv.FormatFloat(buf, v, 'E', -1, 64)
	fmt.Printf("%T, %v\n", s64, s64)

	// fmt.Println uses these arguments to print floats
	fmt64 := strconv.FormatFloat(buf, v, 'g', -1, 64)
	fmt.Printf("%T, %v\n", fmt64, fmt64)

}
Output:
string, 3.1415927E+00
string, 3.1415926535E+00
string, 3.1415926535

func FormatInt

func FormatInt(dst []byte, 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. dst must have enough length to hold the result.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	v := int64(-42)
	buf := make([]byte, 64)

	s10 := strconv.FormatInt(buf, v, 10)
	fmt.Printf("%T, %v\n", s10, s10)

	s16 := strconv.FormatInt(buf, v, 16)
	fmt.Printf("%T, %v\n", s16, s16)

}
Output:
string, -42
string, -2a

func FormatUint

func FormatUint(dst []byte, 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. dst must have enough length to hold the result.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	v := uint64(42)
	buf := make([]byte, 64)

	s10 := strconv.FormatUint(buf, v, 10)
	fmt.Printf("%T, %v\n", s10, s10)

	s16 := strconv.FormatUint(buf, v, 16)
	fmt.Printf("%T, %v\n", s16, s16)

}
Output:
string, 42
string, 2a

func Itoa

func Itoa(dst []byte, i int) string

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

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	i := 10
	buf := make([]byte, 64)
	s := strconv.Itoa(buf, i)
	fmt.Printf("%T, %v\n", s, s)

}
Output:
string, 10

func ParseBool

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.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	v := "true"
	if s, err := strconv.ParseBool(v); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

}
Output:
bool, true

func ParseFloat

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 rounded using IEEE754 unbiased rounding. (Parsing a hexadecimal floating-point value only rounds when there are more bits in the hexadecimal representation than will fit in the mantissa.)

The errors that ParseFloat returns have concrete type *NumError and include err.Num = s.

If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.

If s is syntactically well-formed but is more than 1/2 ULP away from the largest floating point number of the given size, ParseFloat returns f = ±Inf, err.Err = ErrRange.

ParseFloat recognizes the string "NaN", and the (possibly signed) strings "Inf" and "Infinity" as their respective special floating point values. It ignores case when matching.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	v := "3.1415926535"
	if s, err := strconv.ParseFloat(v, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat(v, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("NaN", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	// ParseFloat is case insensitive
	if s, err := strconv.ParseFloat("nan", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("inf", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("+Inf", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("-Inf", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("-0", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("+0", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

}
Output:
float64, 3.1415927410125732
float64, 3.1415926535
float64, NaN
float64, NaN
float64, +Inf
float64, +Inf
float64, -Inf
float64, -0
float64, 0

func ParseInt

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 specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64. If bitSize is below 0 or above 64, an error is returned.

The errors that ParseInt returns have concrete type [*NumError] and include err.Num = s. If s is empty or contains invalid digits, err.Err = ErrSyntax and the returned value is 0; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange and the returned value is the maximum magnitude integer of the appropriate bitSize and sign.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	v32 := "-354634382"
	if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

	v64 := "-3546343826724305832"
	if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

}
Output:
int64, -354634382
int64, -3546343826724305832

func ParseUint

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

ParseUint is like ParseInt but for unsigned numbers.

A sign prefix is not permitted.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strconv"
)

func main() {
	v := "42"
	if s, err := strconv.ParseUint(v, 10, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseUint(v, 10, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

}
Output:
uint64, 42
uint64, 42

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL