如果第一个失败,Go 是否执行“if”中的所有条件?

问题描述

想象一下下面的一段代码

if someBool && funcReturningABool() {
    // code here...
}

其中 someBool 是布尔值,funcReturningABool 返回 true 或 false。

如果someBool等于false,funcReturningABool还会被执行吗?

解决方法

正如您在 the Golang documentation 中所见:

有条件地评估右操作数

,

不会,而且不仅适用于 golang。

enter image description here