Golang函数结果的逻辑与运算

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

Golang Logical AND of result of function

问题

我想多次运行一个函数,如果有任何失败的情况发生,我想执行一些操作。

所以在循环调用之前,我设置了一个标志,被调用的函数返回一个成功/失败的布尔值,但是,如果我使用内联逻辑与(logical AND),函数将不再被调用,就好像存在一个IF语句一样。有人可以解释一下这是为什么吗?

以下是一个在GO Playground中的示例(https://go.dev/play/p/I5DP2mDmoqI):

package main

import "fmt"

func main() {
    fmt.Println("In-line logical AND")
    resultOK := true
    for i := 0; i < 10; i++ {
        resultOK = resultOK && doSomething(i)
    }

    fmt.Println("Separate line for logical AND")
    resultOK = true
    for i := 0; i < 10; i++ {
        result := doSomething(i)
        resultOK = resultOK && result
    }

}

func doSomething(i int) bool {
    fmt.Println("Hello, 世界", i)
    return i%2 == 0
}

运行结果如下:

In-line logical AND
Hello, 世界0 :  true
Hello, 世界1 :  false
 :  false
 :  false
 :  false
 :  false
 :  false
 :  false
 :  false
 :  false
Separate line for logical AND
Hello, 世界0 :  true
Hello, 世界1 :  false
Hello, 世界2 :  false
Hello, 世界3 :  false
Hello, 世界4 :  false
Hello, 世界5 :  false
Hello, 世界6 :  false
Hello, 世界7 :  false
Hello, 世界8 :  false
Hello, 世界9 :  false
英文:

I want to run a function multiple times and then do something if anything failed.

So I set a flag before looping the calls, the called function comes back with a success/failure boolean but, if I use an in-line logical AND, the function is no longer called as though an IF statement exists. Can someone explain this?

Example in GO Playground (https://go.dev/play/p/I5DP2mDmoqI)

package main

import &quot;fmt&quot;

func main() {
	fmt.Println(&quot;In-line logical AND&quot;)
	resultOK := true
	for i := 0; i &lt; 10; i++ {
		resultOK = resultOK &amp;&amp; doSomething(i)
	}

	fmt.Println(&quot;Separate line for logical AND&quot;)
	resultOK = true
	for i := 0; i &lt; 10; i++ {
		result := doSomething(i)
		resultOK = resultOK &amp;&amp; result
	}

}

func doSomething(i int) bool {
	fmt.Println(&quot;Hello, 世界&quot;, i)
	return i%2 == 0

}

Resulting output

In-line logical AND
Hello, 世界0 :  true
Hello, 世界1 :  false
 :  false
 :  false
 :  false
 :  false
 :  false
 :  false
 :  false
 :  false
Separate line for logical AND
Hello, 世界0 :  true
Hello, 世界1 :  false
Hello, 世界2 :  false
Hello, 世界3 :  false
Hello, 世界4 :  false
Hello, 世界5 :  false
Hello, 世界6 :  false
Hello, 世界7 :  false
Hello, 世界8 :  false
Hello, 世界9 :  false

答案1

得分: 3

区别在于短路求值

如果逻辑与运算符的左操作数为false,结果将为false,因此右操作数不会被计算。在你的第一个示例中,右操作数是函数调用,所以函数不会被调用。同样,如果逻辑或运算符的第一个操作数为true,结果为true,那么在这种情况下右操作数也不会被计算。

这在规范:逻辑运算符中有提到:

逻辑运算符适用于布尔值,并产生与操作数相同类型的结果。右操作数是有条件地进行评估的。

在第二个示例中,函数在逻辑与之前被调用,所以无论resultOK是什么,函数都会被调用。

英文:

The difference is short-circuit evaluation.

If the left operand of the logical AND is false, the result will be false, so the right operand is not evaluated. In your first example the right operand is the function call, so the function will NOT be called. Similarly, if the first operand of a logical OR is true, the result is true, so the right operand in that case would not be evaluated either.

This is mentioned in Spec: Logical operators:

> Logical operators apply to boolean values and yield a result of the same type as the operands. The right operand is evaluated conditionally.

In the second example the function is called before the logical AND, so it is called no matter what resultOK is.

huangapple
  • 本文由 发表于 2022年8月1日 20:36:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/73193772.html
匿名

发表评论

匿名网友

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

确定