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

huangapple go评论88阅读模式
英文:

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

问题

我正在尝试在这个小程序中获取用户输入。我尝试了几种方法,使用了strconv.Atoi()方法(我的输入显然是一个字符串,我想将其转换为整数)。这是我的第一次尝试:

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)
}

在编译时,我得到了以下错误:

(第19行) strconv.Atoi() 的多值在单值上下文中

然后我查看了Godocs并试图自己解决这个问题,然后意识到还返回了一个错误值。所以,我将

input := strconv.Atoi(inputstr)

改为

input, _ := strconv.Atoi(inputstr)

现在这个程序可以编译通过,没有任何错误。然而,当我运行程序时,我得到了以下结果:

Say something, in numbers.

0

然后程序就退出了...我做错了什么?我认为这是关于Atoi()方法的问题,但如果是关于Scanln()的问题,请纠正我。

英文:

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.

答案1

得分: 11

问题的原因是Scanln函数。Scanln返回一个错误type not a pointer,这是因为使用了%s。这导致inputstr为空,当传递给Atoi函数时,会返回错误:strconv.ParseInt: parsing "": invalid syntax

可以使用以下方式使用Scanf函数,而不对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)
}

可能最简单的解决方案是从Scanln函数中删除"%s"。

英文:

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.

huangapple
  • 本文由 发表于 2013年10月23日 09:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/19531406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定