Introduction
Operator precedence tells the computer which math or logic steps to do first when there are many in one line.
Go uses a fixed order of precedence for operators. For example:
* and / have higher precedence than + and -
== and != have lower precedence than arithmetic operators
&& has higher precedence than ||
Parentheses () can be used to change the order.result := 3 + 4 * 2
result := (3 + 4) * 2
flag := true || false && false
package main import "fmt" func main() { a := 3 + 4 * 2 b := (3 + 4) * 2 c := true || false && false fmt.Println("a:", a) fmt.Println("b:", b) fmt.Println("c:", c) }
+, -, *, /?result := 5 + 3 * 2 uses valid operators and correct syntax. Options B, C, and D have syntax errors like misplaced operators or missing parentheses.result := 5 + 3 * 2result := 5 + 3 * 2 [OK]package main
import "fmt"
func main() {
x := 2 + 3 * 4 - 6 / 2
fmt.Println(x)
}package main
import "fmt"
func main() {
result := 10 / 2 * 3
+ 4 - 1
fmt.Println(result)
}result := 8 + 2 * (5 - 3) / 2 - 1
result after evaluation?Pass the test and this pattern moves unknown