Languages: Python C C++ Go Java Ruby Rust Perl R ← Full TOC

← Go Index

🔢 strconv — String ↔ primitive type conversions

Converts between strings and basic data types: integers, floats, and booleans. Essential for parsing command-line arguments, config files, and HTTP query parameters. All parse functions return both a value and an error — always check the error.

Import: import "strconv" | Docs: pkg.go.dev/strconv

📋 Functions, Types & Constants

NameSignature / ValueDescription
strconv.Atoifunc Atoi(s string) (int, error)Parse decimal string to int (shorthand for ParseInt base 10)
strconv.Itoafunc Itoa(i int) stringConvert int to decimal string
strconv.ParseIntfunc ParseInt(s string, base, bitSize int) (int64, error)Parse string to int64 in given base (2–36)
strconv.ParseUintfunc ParseUint(s string, base, bitSize int) (uint64, error)Parse string to uint64
strconv.ParseFloatfunc ParseFloat(s string, bitSize int) (float64, error)Parse string to float64
strconv.ParseBoolfunc ParseBool(s string) (bool, error)Parse '1','t','T','TRUE','true','TRUE','0','f','F' etc.
strconv.FormatIntfunc FormatInt(i int64, base int) stringFormat int64 as string in given base
strconv.FormatFloatfunc FormatFloat(f float64, fmt byte, prec, bitSize int) stringFormat float64 as string
strconv.FormatBoolfunc FormatBool(b bool) stringReturn 'true' or 'false'
strconv.AppendIntfunc AppendInt(dst []byte, i int64, base int) []byteAppend int64 string to byte slice
strconv.Quotefunc Quote(s string) stringReturn double-quoted Go string literal
strconv.Unquotefunc Unquote(s string) (string, error)Interpret s as a quoted Go string literal
strconv.ErrRangevar ErrRange errorReturned when value out of range for target type
strconv.ErrSyntaxvar ErrSyntax errorReturned when string not in expected format

💡 Example Program

Save as main.go inside any directory and run with go run main.go. No extra packages to install — all standard library.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    // String → int
    n, err := strconv.Atoi("42")
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Printf("Atoi: %d  (type %T)\n", n, n)
    }

    // int → string
    s := strconv.Itoa(12345)
    fmt.Printf("Itoa: %q\n", s)

    // ParseFloat
    f, err := strconv.ParseFloat("3.14159", 64)
    if err == nil {
        fmt.Printf("ParseFloat: %.5f\n", f)
    }

    // ParseBool
    b, _ := strconv.ParseBool("true")
    fmt.Printf("ParseBool: %v\n", b)

    // ParseInt with base
    hex, _ := strconv.ParseInt("FF", 16, 64)
    bin, _ := strconv.ParseInt("1010", 2, 64)
    fmt.Printf("ParseInt hex=0xFF → %d\n", hex)
    fmt.Printf("ParseInt bin=1010 → %d\n", bin)

    // FormatFloat
    fs := strconv.FormatFloat(3.14159265, 'f', 4, 64)
    fmt.Println("FormatFloat:", fs)

    // Error handling best practice
    if _, err := strconv.Atoi("not_a_number"); err != nil {
        fmt.Println("Expected error:", err)
    }

    // Quote / Unquote
    quoted := strconv.Quote("Hello\tWorld\n")
    fmt.Println("Quoted:", quoted)
    unquoted, _ := strconv.Unquote(quoted)
    fmt.Println("Unquoted:", unquoted)
}
// go run main.go

← strings  |  🏠 Index  |  math →