a comparison equals to true, but when i put it as if condition, why it is not evaluated as true?

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

a comparison equals to true, but when i put it as if condition, why it is not evaluated as true?

问题

以下是翻译好的内容:

a := "ALL"
b := "not all"

if (a == "ALL") != (b == "ALL") {
    fmt.Printf("为什么没有进入这个条件分支?\n")
}

这是我写的一段 Go 代码,如果 a 和 b 中只有一个等于 "ALL",则打印一些内容。但为什么 fmt.Printf 没有触发呢?

然而,下面的代码确实会打印:

c := (a == "ALL") != (b == "ALL")
if c {
    fmt.Printf("如果将结果赋值给 c,就会进入这里\n")
}

但是这两段代码有什么区别呢?

我还在 Go Playground 上粘贴了上述代码:http://play.golang.org/p/e5tNai_d20。非常感谢任何人能解释一下!提前感谢!

英文:
a := "ALL"
b := "not all"

if (a == "ALL") != (b == "ALL") {
    fmt.Printf("why not falling into this case?\n")
}

Here is a piece of Go code I wrote, to print something if a and b have exactly one to be "ALL". But why the fmt.Printf is not triggered?

However the following does print:

c := (a == "ALL") != (b == "ALL")
if c {
	fmt.Printf("if assign to c, do fall in here\n")
}

But what's the difference between the two pieces?

I also pasted the above code in the go playground: http://play.golang.org/p/e5tNai_d20 Anyone's explanation will be greatly appreciated! Thanks in advance.

答案1

得分: 7

这是一个Go 1.5的bug,与这个问题相似:https://github.com/golang/go/issues/12226

该问题已在主分支上修复,所以你可以降级到1.4.2并等待下一个版本发布,或者编译主分支的最新代码。

编辑:Go 1.5.1已发布,其中包含了修复此问题的补丁。

英文:

It is a go 1.5 bug, and looks similiar to this one : https://github.com/golang/go/issues/12226

It is fixed on the master branch, so you can either downgrade to 1.4.2 and wait for the next release, or compile the tip of master.

Edit : Go1.5.1 has been released and it contains the fix to this issue

huangapple
  • 本文由 发表于 2015年8月30日 17:11:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/32294975.html
匿名

发表评论

匿名网友

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

确定