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.