527

I'm trying to convert a string returned from flag.Arg(n) to an int. What is the idiomatic way to do this in Go?

2
  • 8
    strconv.Itoa(i) (int to ASCII) to set an int to a string. See stackoverflow.com/a/62737936/12817546. strconv.Atoi(s) (ASCII to int) to set a string to an int. See stackoverflow.com/a/62740786/12817546. Commented Jul 9, 2020 at 9:05
  • "set an int to a string" is not a good description. "returns a string given an integer" makes more sense. It doesn't actually set anything. Commented Dec 5, 2024 at 4:12

6 Answers 6

657

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)
}
Sign up to request clarification or add additional context in comments.

6 Comments

strconv can be interpreted as String Convert likewise, is there any meaning (extension) for Atoi to make it easier to remember.
@RoshanaPitigala "A" as in A-to-Z for the alphabet which is represented by strings. And then "i" for integer. A to i. That's one way. Another way is to know the origin of "atoi" which is apparently "ASCII to integer": stackoverflow.com/a/2909772/1544473
It is a shame that in a language created WELL into the era of UNICODE, the best mnemonic the creators of Go could think of were "Alphabet-to-Integer" (which is misleading; decimal digits are not alphabet characters) or "ASCII-to-Integer" (which is obsolete and will someday soon be remanded to the dustbin of history). "Character-to-Integer", "String-to-Integer", or even "Unicode-to-Integer" would have been MUCH better mnemonics, IMO. On reflection it seems like they were just mimicking the C language convention which includes an atoi() library function.
Of course they would mimick the C language convention. This is a good practice, it follows the principle of least surprise.
ParseInt would be very easy to understand and exists in many languages
@Aidan: Go also has strconv.ParseInt() which takes a string, a base, and integer size. Atoi(s) is equivalent to ParseInt(s, 10, 0) (which is actually mentioned right on the page this answer links to). @Roger Glover: In addition to @MarioVilas's comment, the only supported digits are in fact ASCII. I don't think it's reasonable to expect the function to convert all of the various non-ASCII digit characters like circled digits, superscript/subscript digits, etc.
158

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
}

4 Comments

What does the second argument to ParseInt specify?
@kaushik94 Click on the strconv.ParseInt() link and you'll see immediately: ParseInt(s string, base int, bitSize int). So it's the base: "ParseInt interprets a string s in the given base (2 to 36) "
Note that the bitSize argument to strconv.ParseInt() will not convert the string to your choice of type but instead is only there to confine the result to a specific 'bitness'. See also: stackoverflow.com/questions/55925894/…
@viv Yes, that's correct. If a value of type int is required and strconv.ParseInt() is used, manual type conversion is needed (from int64 to int).
73

Here are three ways to parse strings into integers, from fastest runtime to slowest:

  1. strconv.ParseInt(...) fastest
  2. strconv.Atoi(...) still very fast
  3. fmt.Sscanf(...) not terribly fast but most flexible

Here's a benchmark that shows usage and example timing for each function:

package main

import "fmt"
import "strconv"
import "testing"

var num = 123456
var numstr = "123456"

func BenchmarkStrconvParseInt(b *testing.B) {
  num64 := int64(num)
  for i := 0; i < b.N; i++ {
    x, err := strconv.ParseInt(numstr, 10, 64)
    if x != num64 || err != nil {
      b.Error(err)
    }
  }
}

func BenchmarkAtoi(b *testing.B) {
  for i := 0; i < b.N; i++ {
    x, err := strconv.Atoi(numstr)
    if x != num || err != nil {
      b.Error(err)
    }
  }
}

func BenchmarkFmtSscan(b *testing.B) {
  for i := 0; i < b.N; i++ {
    var x int
    n, err := fmt.Sscanf(numstr, "%d", &x)
    if n != 1 || x != num || err != nil {
      b.Error(err)
    }
  }
}

You can run it by saving as atoi_test.go and running go test -bench=. atoi_test.go.

goos: darwin
goarch: amd64
BenchmarkStrconvParseInt-8      100000000           17.1 ns/op
BenchmarkAtoi-8                 100000000           19.4 ns/op
BenchmarkFmtSscan-8               2000000          693   ns/op
PASS
ok      command-line-arguments  5.797s

2 Comments

Clear and helpful answer. I quoted you here stackoverflow.com/a/62740786/12817546.
Atoi is equivalent to ParseInt(s, 10, 0), converted to type int. pkg.go.dev/strconv#Atoi
9

Try this

import ("strconv")

value := "123"
number,err := strconv.ParseUint(value, 10, 32)
finalIntNum := int(number) //Convert uint64 To int

1 Comment

This code will silently (no error) transform values which are in the uint32 range but not in the int range on platforms where int is 32 bits. Example value: "4234567890" is converted to -60399406. Go Playground (64 bits)
1

If you control the input data, you can use the mini version

package main

import (
    "testing"
    "strconv"
)

func Atoi (s string) int {
    var (
        n uint64
        i int
        v byte
    )   
    for ; i < len(s); i++ {
        d := s[i]
        if '0' <= d && d <= '9' {
            v = d - '0'
        } else if 'a' <= d && d <= 'z' {
            v = d - 'a' + 10
        } else if 'A' <= d && d <= 'Z' {
            v = d - 'A' + 10
        } else {
            n = 0; break        
        }
        n *= uint64(10) 
        n += uint64(v)
    }
    return int(n)
}

func BenchmarkAtoi(b *testing.B) {
    for i := 0; i < b.N; i++ {
        in := Atoi("9999")
        _ = in
    }   
}

func BenchmarkStrconvAtoi(b *testing.B) {
    for i := 0; i < b.N; i++ {
        in, _ := strconv.Atoi("9999")
        _ = in
    }   
}

the fastest option (write your check if necessary). Result :

Path>go test -bench=. atoi_test.go
goos: windows
goarch: amd64
BenchmarkAtoi-2                 100000000               14.6 ns/op
BenchmarkStrconvAtoi-2          30000000                51.2 ns/op
PASS
ok      path     3.293s

6 Comments

What ? Really ? People who wrote "go" made a lot easy. Dont spin your wheel :)
What about Atoi("-9999")?
This implementation lacks documentation that would mentions the not handled edge cases (ex: negative decimal integers). And I'm not even mentioning the lack of tests.
If anybody is tempted to use this despite the comments above be aware that the base is hardcoded to 10 but it decodes letters as if it were an arbitrary base. This will give strange results if you try to decode hex with it.
A correct benchmark would include all kinds of numbers to see whether there is a difference between decimals and hex. Also hex. is limited to 'F' (not 'Z') and requires 'n *= 16' not 10...
|
0

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, uint32, int64, uint64 data type. faiNumber also supports converting an int32, uint32, int64, or uint64 values to a decimal string, a binary string, an octal string, or a hex string.

You can download faiNumber from https://github.com/kevinhng86/faiNumber-Go. After download, put the faiNumber folder in your module path and import using <module_path>/faiNumber. Or you can install the faiNumber package using "go install github.com/kevinhng86/faiNumber-Go/faiNumber@latest" then using "go get github.com/kevinhng86/faiNumber-Go/faiNumber" in your module directory. If you choose to install faiNumber using go install, then you can keep the same import path

The documentation for faiNumber can be found here https://lib.fai.host/go/github.com/kevinhng86/faiNumber-Go/faiNumber/ .

I wrote faiNumber and hope that one day it will make it to the standard library.

Below is the code example for using faiNumber to parse a string of decimal to either an int32, a uint32, an int64, or a uint64 value.


package main
import "fmt"
import "github.com/kevinhng86/faiNumber-Go/faiNumber"

func main() {
    var nInt32 int32
    var nUint32 uint32
    var nInt64 int64
    var nUint64 uint64
    var err error

    str := "123"

    // Decimal string to int32
    nInt32, err = faiNumber.DecToInt32(str)
    if err != nil {
        // Error handling
        fmt.Println(err)
    } else {
        // No error path
        fmt.Printf("String: \"%s\", int32: %d\n\n", str, nInt32)
    }

    // Decimal string to uint32, sign prefix is not permitted
    nUint32, err = faiNumber.DecToUInt32(str)
    if err != nil {
        // Error handling
        fmt.Printf("FromFunction: %s, Input: \"%s\", Error Code: %d, Error Message: %s.\n\n",
                   err.(*faiNumber.FNError).FromFunction, err.(*faiNumber.FNError).Input,
                   err.(*faiNumber.FNError).Code, err.(*faiNumber.FNError).Message)
    } else {
        // No error path
        fmt.Printf("String: \"%s\", uint32: %d\n\n", str, nUint32)
    }

    // Decimal string to int64
    nInt64, err = faiNumber.DecToInt64(str)
    if err != nil {
        // Error handling
        fmt.Println(err)
    } else {
        // No error path
        fmt.Printf("String: \"%s\", int64: %d\n\n", str, nInt64)
    }

    // Decimal string to uint64, sign prefix is not permitted
    nUint64, err = faiNumber.DecToUInt64(str)
    if err != nil {
        // Error handling
        fmt.Println(err)
    } else {
        // No error path
        fmt.Printf("String: \"%s\", uint64: %d\n\n", str, nUint64)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.