例子:
if function1(input) && function2(input) {}
在这种情况下,如果 function1(input)
返回 false,是否会执行 function2(input)
?
最佳答案
你问的是Short Circuiting,是的,Go 做到了。
在language spec,上面写着
Logical operators apply to boolean values and yield a result of the same type as the operands. The right operand is evaluated conditionally.
这意味着,在您的情况下,如果 function1
返回 false,则不会调用 function2
。
关于if-statement - go 是否执行 if 语句中的所有条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45698298/