🌐
Go Packages
pkg.go.dev › strconv
strconv package - strconv - Go Packages
The exponent is written as a decimal integer; for all formats other than 'b', it will be at least two digits. ... package main import ( "fmt" "strconv" ) func main() { v := 3.1415926535 s32 := strconv.FormatFloat(v, 'E', -1, 32) fmt.Printf("%T, %v\n", s32, s32) s64 := strconv.FormatFloat(v, 'E', -1, 64) fmt.Printf("%T, %v\n", s64, s64) // fmt.Println uses these arguments to print floats fmt64 := strconv.FormatFloat(v, 'g', -1, 64) fmt.Printf("%T, %v\n", fmt64, fmt64) }
🌐
Educative
educative.io › answers › how-to-use-and-implement-strconv-package-in-golang
How to use and implement strconv package in golang
Now, let’s use the function to convert a string to an integer value. Here, I am going to demonstrate how to use the strconv package with the Atoi function to convert a string.
Discussions

Convert string to integer type in Go? - Stack Overflow
If you need to parse strings really fast, you can use faiNumber package. faiNumber is the fastest golang string parser library. All of faiNumber's function was benchmark to run way faster than the strconv package. faiNumber supports parsing strings of decimal, binary, octal, hex to an int32, ... More on stackoverflow.com
🌐 stackoverflow.com
How to Convert uint64 Fields to Strings in Go Structs?
https://pkg.go.dev/encoding/json#Marshal The "string" option signals that a field is stored as JSON inside a JSON-encoded string. It applies only to fields of string, floating point, integer, or boolean types. This extra level of encoding is sometimes used when communicating with JavaScript programs: Int64String int64 `json:",string"` More on reddit.com
🌐 r/golang
12
5
December 19, 2024
How to change integer to string with go in this case?
c.Header("X-Total-Count", strconv.Itoa(count))
More on reddit.com
🌐 r/golang
4
0
June 18, 2018
strconv.Atoi is causing my test to fail and I can't figure out why
Your errors.New(whatever) is distinct/not equal to any other errors.New(whatever) as documented here : New returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical. More on reddit.com
🌐 r/golang
9
1
April 1, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › go language › strconv-package-in-golang
strconv package in Golang - GeeksforGeeks
July 15, 2025 - To access the functions of the strconv package you need to import the strconv package in your program with the help of the import keyword. Example 1: go · // Golang program to illustrate the // strconv.AppendQuoteRuneToASCII() function package ...
🌐
Go by Example
gobyexample.com › number-parsing
Go by Example: Number Parsing
Parsing numbers from strings is a basic but common task in many programs; here’s how to do it in Go · The built-in package strconv provides the number parsing
🌐
7-Zip Documentation
documentation.help › Golang › strconv.htm
strconv - The Go Programming Language - Golang Documentation
AppendQuoteToASCII appends a double-quoted Go string literal representing s, as generated by QuoteToASCII, to dst and returns the extended buffer. func AppendUint(dst []byte, i uint64, base int) []byte
🌐
Reintech
reintech.io › blog › a-guide-to-gos-strconv-package-converting-strings-and-numbers
A Guide to Go's `strconv` Package: Converting Strings and Numbers | Reintech media
January 26, 2026 - Learn how to use Go's strconv package to convert strings to and from various numeric types, including integers, floats, and booleans, with this in-depth tutorial.
🌐
Reintech
reintech.io › blog › introduction-to-gos-strconv-package-string-conversions
An Introduction to Go's `strconv` Package: String Conversions | Reintech media
January 26, 2026 - Learn how to use Go's strconv package to perform string conversions, converting strings to and from other data types like integers and floats.
Top answer
1 of 6
661

For example strconv.Atoi.

Code:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := "123"

    // string to int
    i, err := strconv.Atoi(s)
    if err != nil {
        // ... handle error
        panic(err)
    }

    fmt.Println(s, i)
}
2 of 6
160

Converting Simple strings

The easiest way is to use the strconv.Atoi() function.

Note that there are many other ways. For example fmt.Sscan() and strconv.ParseInt() which give greater flexibility as you can specify the base and bitsize for example. Also as noted in the documentation of strconv.Atoi():

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

Here's an example using the mentioned functions (try it on the Go Playground):

flag.Parse()
s := flag.Arg(0)

if i, err := strconv.Atoi(s); err == nil {
    fmt.Printf("i=%d, type: %T\n", i, i)
}

if i, err := strconv.ParseInt(s, 10, 64); err == nil {
    fmt.Printf("i=%d, type: %T\n", i, i)
}

var i int
if _, err := fmt.Sscan(s, &i); err == nil {
    fmt.Printf("i=%d, type: %T\n", i, i)
}

Output (if called with argument "123"):

i=123, type: int
i=123, type: int64
i=123, type: int

Parsing Custom strings

There is also a handy fmt.Sscanf() which gives even greater flexibility as with the format string you can specify the number format (like width, base etc.) along with additional extra characters in the input string.

This is great for parsing custom strings holding a number. For example if your input is provided in a form of "id:00123" where you have a prefix "id:" and the number is fixed 5 digits, padded with zeros if shorter, this is very easily parsable like this:

s := "id:00123"

var i int
if _, err := fmt.Sscanf(s, "id:%5d", &i); err == nil {
    fmt.Println(i) // Outputs 123
}
Find elsewhere
🌐
Medium
medium.com › go-walkthrough › go-walkthrough-strconv-7a24632a9e73
Go Walkthrough: strconv. Fast, efficient, & safe primitive… | by Ben Johnson | Go Walkthrough | Medium
August 1, 2017 - The strconv package gives us a way to format our primitives quickly and efficiently while providing some basic formatting options. It also preserves strong type checking for its arguments whereas fmt frequently uses interface{}. If you liked this, click the💚 below so other people will see this here on Medium. Programming · Golang ·
🌐
GeeksforGeeks
geeksforgeeks.org › strconv-atoi-function-in-golang-with-examples
strconv.Atoi() Function in Golang With Examples - GeeksforGeeks
April 21, 2020 - The Store() function in Go language is used to set the value of the Value to x(i.e, interface). And all the calls to Store method for a stated Value should use values of an id ...
🌐
Coding Explorations
codingexplorations.com › blog › string-manipulation-made-easy-with-strings-and-strconv-in-go
String Manipulation Made Easy with strings and strconv in Go — Coding Explorations
August 16, 2023 - The strconv.FormatFloat function ... and bit size. package main import ( "fmt" "strconv" ) func main() { num := 3.14159 str := strconv.FormatFloat(num, 'f', 2, 64) fmt.Println(str) // Output: "3.14" }...
🌐
Go Packages
pkg.go.dev › cuelang.org › go › pkg › strconv
strconv package - cuelang.org/go/pkg/strconv - Go Packages
March 3, 2026 - FormatInt returns the string representation of i in the given base, for 2 <= base <= 62. The result uses: For 10 <= digit values <= 35, the lower-case letters 'a' to 'z' For 36 <= digit values <= 61, the upper-case letters 'A' to 'Z'
🌐
Dot Net Perls
dotnetperls.com › parseint-go
Go - ParseInt Examples: Convert String to Int - Dot Net Perls
package main import ( "fmt" "strconv" ) func main() { value := "123" // Convert string to int.
🌐
KodeKloud Notes
notes.kodekloud.com › docs › Golang › Data-Types-and-Variables › Converting-between-types › page
Converting between types - KodeKloud
package main import ( "fmt" "strconv" ) func main() { var i int = 42 var s string = strconv.Itoa(i) // convert int to string fmt.Printf("%q", s) }
🌐
Educative
educative.io › answers › how-to-use-the-strconvparseint-function-in-golang
How to use the strconv.ParseInt() function in Golang
The strconv package’s ParseInt() function converts the given string s to an integer value i in the provided base (0, 2 to 36) and bit size (0 to 64).
🌐
Go
go.dev › src › archive › tar › strconv.go
strconv.go
204 ss, sn, _ := strings.Cut(s, ".") 205 206 // Parse the seconds. 207 secs, err := strconv.ParseInt(ss, 10, 64) 208 if err != nil { 209 return time.Time{}, ErrHeader 210 } 211 if len(sn) == 0 { 212 return time.Unix(secs, 0), nil // No sub-second values 213 } 214 215 // Parse the nanoseconds.
🌐
Golang.cafe
golang.cafe › blog › golang-int-to-string-conversion-example
Golang Int To String Conversion Example | Golang.cafe
April 27, 2022 - Let’s see an example on how we can use these functions to convert an integer to an ASCII string · package main import ( "fmt" "strconv" ) func main() { i := 10 s1 := strconv.FormatInt(int64(i), 10) s2 := strconv.Itoa(i) fmt.Printf("%v, %v\n", s1, s2) }
🌐
Educative
educative.io › answers › how-to-use-the-strconvparsefloat-function-in-golang
How to use the strconv.ParseFloat() function in Golang
The ParseFloat() function is a strconv package inbuilt function that converts strings to a floating-point number with the precision defined by the bitSize. The value of bitSize must be 32 or 64.
🌐
Educative
educative.io › answers › how-to-use-the-strconvformatint-function-in-go
How to use the strconv.FormatInt() function in Go
The strconv package’s FormatInt() function is used to obtain the string representation of a given integer in a chosen base, which can range from 2 to 36 (2 <= base <= 36).