Short circuit evaluation
Although in the Go language specification [1] it does not explicitly state that Go uses short circuit evaluation, it does mentiong that
Logical operators apply to boolean values and yield a result of the same type as the operands. The right operand is evaluated conditionally.
Here is a quick example to prove that Go uses short circuit evaluation
// go run short-circuit.go
package main
import "fmt"
func val1() bool{
fmt.Println("val1 gets called")
return true
}
func val2() bool{
fmt.Println("val2 gets called")
return false
}
func main(){
if val1() && val2() {
fmt.Println("Shouldn't print")
}
if val2() && val1() {
fmt.Println("Shouldn't print")
}
if val1() || val2(){
fmt.Println("The boolean expression is true")
}
if val2() || val1(){
fmt.Println("The boolean expression is true")
}
}
You should expect the following result:
val1 gets called
val2 gets called
val2 gets called
val1 gets called
The boolean expression is true
val2 gets called
val1 gets called
The boolean expression is true