strconv.Atoi() / Itoa() / ParseFloat()
The strconv package in Go provides functions for converting between strings and numeric or boolean values. It is commonly used when you need to parse HTTP query parameters or configuration file values into numbers.
Syntax
import "strconv"
// string → int (base 10)
n, err := strconv.Atoi("42")
// int → string
s := strconv.Itoa(42)
// string → float64
f, err := strconv.ParseFloat("3.14", 64) // 64 is the bit size
// string → bool
b, err := strconv.ParseBool("true") // accepts "1","t","T","TRUE","true","True"
// string → int64 (base and bit size can be specified)
n64, err := strconv.ParseInt("FF", 16, 64) // converts hex string to 64-bit integer
// float64 → string
s := strconv.FormatFloat(3.14, 'f', 2, 64) // format 'f', precision 2, 64-bit
// int64 → string (base can be specified)
s := strconv.FormatInt(255, 16) // converts to hexadecimal string
// bool → string
s := strconv.FormatBool(true) // "true"
Function List
| Function | Description |
|---|---|
| strconv.Atoi(s) | Converts string s to a base-10 int. |
| strconv.Itoa(i) | Converts integer i to a base-10 string. |
| strconv.ParseFloat(s, bitSize) | Converts a string to a floating-point number. bitSize must be 32 or 64. |
| strconv.ParseInt(s, base, bitSize) | Converts a string to an int64 with the specified base and bit size. |
| strconv.ParseUint(s, base, bitSize) | Converts a string to an unsigned integer uint64. |
| strconv.ParseBool(s) | Converts a string to a boolean. Accepts values such as "1", "true", and "t". |
| strconv.FormatFloat(f, fmt, prec, bitSize) | Converts a floating-point number to a string. |
| strconv.FormatInt(i, base) | Converts an int64 to a string in the specified base. |
| strconv.FormatBool(b) | Converts a boolean to "true" or "false". |
| strconv.Quote(s) | Escapes a string into Go double-quoted string literal format. |
Sample Code
package main
import (
"fmt"
"strconv"
)
func main() {
// Atoi: string → int
n, err := strconv.Atoi("123")
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Printf("Atoi result: %d (type: %T)\n", n, n)
}
// Error handling for a failed conversion
_, err = strconv.Atoi("abc")
if err != nil {
fmt.Println("Atoi failed:", err) // strconv.Atoi: ...
}
// Itoa: int → string
s := strconv.Itoa(456)
fmt.Printf("Itoa result: %q (type: %T)\n", s, s)
fmt.Println()
// ParseFloat: string → float64
f, _ := strconv.ParseFloat("3.14159", 64)
fmt.Printf("ParseFloat: %.5f\n", f)
// FormatFloat: float64 → string
fs := strconv.FormatFloat(3.14159, 'f', 3, 64) // 3 decimal places
fmt.Println("FormatFloat('f',3):", fs)
fs2 := strconv.FormatFloat(12345.678, 'e', 2, 64) // scientific notation
fmt.Println("FormatFloat('e',2):", fs2)
fmt.Println()
// ParseInt: conversion with base
hex, _ := strconv.ParseInt("FF", 16, 64)
fmt.Printf("hex FF → %d\n", hex)
bin, _ := strconv.ParseInt("1010", 2, 64)
fmt.Printf("binary 1010 → %d\n", bin)
// FormatInt: integer → string in specified base
fmt.Println("255 in binary:", strconv.FormatInt(255, 2))
fmt.Println("255 in hex:", strconv.FormatInt(255, 16))
fmt.Println()
// ParseBool / FormatBool
b, _ := strconv.ParseBool("true")
fmt.Println("ParseBool:", b)
fmt.Println("FormatBool:", strconv.FormatBool(false))
}
Notes
strconv.Atoi() and strconv.Itoa() stand for "ASCII to Integer" and "Integer to ASCII" respectively, and are the most frequently used functions in the strconv package. Always check the error returned on a failed conversion. Unlike simple type conversions (e.g., int(3.14)), strconv handles conversions at the boundary between strings and native types.
strconv.Atoi() returns an int whose size depends on the platform (64-bit on 64-bit environments). If you need an explicit bit width, use strconv.ParseInt(s, 10, 64) to receive the result as an int64.
For string manipulation, see strings.Contains(). For formatted output, see fmt.Println() / Printf().
If you find any errors or copyright issues, please contact us.