Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the strconv.Atoi() method in Go?

Tags:

casting

input

go

I am trying to get a user input in this small program. I have tried doing this several ways with the strconv.Atoi() method (my input is obviously a string, and I'm trying to convert it to an integer). Here's my first attempt:

package main
    import (
        "fmt"
        "strconv"
    )
    
    func main() {
        //fmt.Println(strconv.Itoa)
        fmt.Println("Say something, in numbers.")
        var inputstr string
        fmt.Scanln("%s", &inputstr)
        input := strconv.Atoi(inputstr)
        output := (input * 2)
        outputstr := strconv.Itoa(output)
        fmt.Println(outputstr)
    }

and got the following error when it came to compiling:

(line 19) multiple-value strconv.Atoi() in single-value context

I then looked into Godocs and tried to figure this out for myself, and then realized that an error value is returned as well. So, I changed the

input := strconv.Atoi(inputstr)

to

input, _ := strconv.Atoi(inputstr)

Now this compiles just fine, without any errors. However, when I run the program, here's what I get:

Say something, in numbers.

0

and then it exits... What am I doing wrong? I believe this is a question about to Atoi() method, but if it's concerning the Scanln() then please correct me.

like image 423
Chris Morris Avatar asked Oct 23 '13 01:10

Chris Morris


People also ask

How do I use Strconv in Golang?

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. This function is used to appends true or false according to the value of x, to dst and returns the extended buffer.

How does atoi () work?

The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number.

What is Strconv ITOA Golang?

To convert int to string in Go language, call strconv. Itoa() function and pass the integer as argument to it. The function returns the string formed with the given integer.

How do I cast a string in Golang?

In order to convert string to integer type in Golang, you can use the following methods. You can use the strconv package's Atoi() function to convert the string into an integer value. Atoi stands for ASCII to integer. The Atoi() function returns two values: the result of the conversion, and the error (if any).


1 Answers

The problem turns out to be the Scanln. Scanln is returning an error type not a pointer because of the %s. This then leaves inputstr blank, which when given to Atoi is returning an error: strconv.ParseInt: parsing "": invalid syntax.

Using Scanf as follows with no change to the Atoi:

func main() {
    //fmt.Println(strconv.Itoa)
    fmt.Println("Say something, in numbers.")
    var inputstr string

    //fmt.Scanln("%s", &inputstr)
    _, err := fmt.Scanf("%s", &inputstr)
    if err != nil {
        fmt.Println(err)
    }
    input, e := strconv.Atoi(inputstr)
    if e != nil {
        fmt.Println(e)
    }
    output := (input * 2)
    outputstr := strconv.Itoa(output)
    fmt.Println(outputstr)
}

Probably the simplest solution is to remove the "%s" from the Scanln.

like image 152
miltonb Avatar answered Sep 29 '22 03:09

miltonb