英文:
Why is the answer correct when using Scanf unsafe, but not when handling errors? (Go)
问题
最近我开始学习Go语言。我在JetBrains Academy做了一个简单的任务。
这是一个非常简单的代码:
var number int
fmt.Scanf("%d", &number)
if number > 0 {
fmt.Println("Positive!")
} else if number < 0 {
fmt.Println("Negative!")
} else {
fmt.Println("Zero!")
这段代码运行得很好,但是如果我使用IDE生成的错误处理代码:
var number int
number, err := fmt.Scanf("%d", &number)
if err != nil {
return
}
if number > 0 {
fmt.Println("Positive!")
} else if number < 0 {
fmt.Println("Negative!")
} else {
fmt.Println("Zero!")
}
如果输入一个数字"0",输出将是"Positive!",而不是"Zero!"。
为什么会这样呢?
英文:
Just recently I started to learn Go. I did a simple task for JetBrains Academy.
There is a very simple code:
var number int
fmt.Scanf("%d", &number)
if number > 0 {
fmt.Println("Positive!")
} else if number < 0 {
fmt.Println("Negative!")
} else {
fmt.Println("Zero!")
This code works just fine, but if I use the auto-generated error handling from the IDE:
var number int
number, err := fmt.Scanf("%d", &number)
if err != nil {
return
}
if number > 0 {
fmt.Println("Positive!")
} else if number < 0 {
fmt.Println("Negative!")
} else {
fmt.Println("Zero!")
}
If you enter a number "0", the output will be "Positive!", not "Zero!"
Why is that?
答案1
得分: 3
number, err := fmt.Scanf("%d", &number)
这将首先将解析的值存储到Scanf
调用中的number
变量中。然后,Scanf
将返回,并且您的代码将将1
存储到number
变量中,因为这是成功扫描的项目数(请参阅文档中Scanf
的返回值)。
要修复这个问题,您不应该同时使用同一个变量来存储解析的结果和Scanf
的返回值。此外,在编写和调试代码时,根据其预期用途准确地命名变量将有助于避免混淆,例如:
parsedItems, err := fmt.Scanf("%d", &number)
英文:
> number, err := fmt.Scanf("%d", &number)
This will first store the parsed value into number
inside the call of Scanf
. Then Scanf
will return and your code will store 1
into number
, since this is the number of items successfully scanned (see the documentation what Scanf
returns).
To fix you should not use the same variable both for storing the parsed result and for storing the return value of Scanf
. Also it helps a lot to name the variables precisely according to their intended use in order to avoid confusion when writing and debugging the code, i.e.
parsedItems, err := fmt.Scanf("%d", &number)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论