Why is the answer correct when using Scanf unsafe, but not when handling errors? (Go)

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

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(&quot;%d&quot;, &amp;number)

	if number &gt; 0 {
		fmt.Println(&quot;Positive!&quot;)
	} else if number &lt; 0 {
		fmt.Println(&quot;Negative!&quot;)
	} else {
		fmt.Println(&quot;Zero!&quot;)

This code works just fine, but if I use the auto-generated error handling from the IDE:

	var number int
	number, err := fmt.Scanf(&quot;%d&quot;, &amp;number)
	if err != nil {
		return
	}

	if number &gt; 0 {
		fmt.Println(&quot;Positive!&quot;)
	} else if number &lt; 0 {
		fmt.Println(&quot;Negative!&quot;)
	} else {
		fmt.Println(&quot;Zero!&quot;)
	}

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(&quot;%d&quot;, &amp;number)

huangapple
  • 本文由 发表于 2022年7月16日 20:43:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/73004319.html
匿名

发表评论

匿名网友

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

确定