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

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

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

问题

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

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. //fmt.Println(strconv.Itoa)
  8. fmt.Println("Say something, in numbers.")
  9. var inputstr string
  10. fmt.Scanln("%s", &inputstr)
  11. input := strconv.Atoi(inputstr)
  12. output := (input * 2)
  13. outputstr := strconv.Itoa(output)
  14. fmt.Println(outputstr)
  15. }

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

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

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

  1. input := strconv.Atoi(inputstr)

改为

  1. 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:

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. //fmt.Println(strconv.Itoa)
  8. fmt.Println("Say something, in numbers.")
  9. var inputstr string
  10. fmt.Scanln("%s", &inputstr)
  11. input := strconv.Atoi(inputstr)
  12. output := (input * 2)
  13. outputstr := strconv.Itoa(output)
  14. fmt.Println(outputstr)
  15. }

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

  1. input := strconv.Atoi(inputstr)

to

  1. 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函数进行任何更改:

  1. func main() {
  2. //fmt.Println(strconv.Itoa)
  3. fmt.Println("Say something, in numbers.")
  4. var inputstr string
  5. //fmt.Scanln("%s", &inputstr)
  6. _, err := fmt.Scanf("%s", &inputstr)
  7. if err != nil {
  8. fmt.Println(err)
  9. }
  10. input, e := strconv.Atoi(inputstr)
  11. if e != nil {
  12. fmt.Println(e)
  13. }
  14. output := (input * 2)
  15. outputstr := strconv.Itoa(output)
  16. fmt.Println(outputstr)
  17. }

可能最简单的解决方案是从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:

  1. func main() {
  2. //fmt.Println(strconv.Itoa)
  3. fmt.Println("Say something, in numbers.")
  4. var inputstr string
  5. //fmt.Scanln("%s", &inputstr)
  6. _, err := fmt.Scanf("%s", &inputstr)
  7. if err != nil {
  8. fmt.Println(err)
  9. }
  10. input, e := strconv.Atoi(inputstr)
  11. if e != nil {
  12. fmt.Println(e)
  13. }
  14. output := (input * 2)
  15. outputstr := strconv.Itoa(output)
  16. fmt.Println(outputstr)
  17. }

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:

确定