Home > Article > Backend Development > Go language operator priority sorting list
Overview of Go language operator priority sorting, specific code examples are required
In Go language, certain priority rules need to be followed when using operators to ensure Expressions are evaluated in the correct order. This article will introduce the precedence of various operators in the Go language and provide corresponding code examples.
Sample code:
package main
import "fmt"
func main() {
result := (2 + 3) * 4 fmt.Println(result) // 输出:20
}
In the above example code, the parentheses change the priority of the result of the addition operation, and the addition operation is calculated before the multiplication operation.
Sample code:
package main
import "fmt"
func main() {
result := -5 + 3 fmt.Println(result) // 输出:-2
}
In the above example code, the negative sign operator changes the sign of the value.
Sample code:
package main
import "fmt"
func main() {
result := 6 / 3 * 2 fmt.Println(result) // 输出:4
}
In the above example code, the integer division operation of 6 divided by 3 is first performed, and then multiplied by 2 to obtain the final result.
Sample code:
package main
import "fmt"
func main() {
result := 5 - 3 + 2 fmt.Println(result) // 输出:4
}
In the above example code, first subtract 3 from 5, and then add 2 to get the final result.
Sample code:
package main
import "fmt"
func main() {
result := 3 + 2 < 7 fmt.Println(result) // 输出:true
}
In the above example code, first calculate the result of 3 plus 2, 5, and then determine whether 5 is less than 7, and the final result is true.
Sample code:
package main
import "fmt"
func main() {
result := (3 + 2 < 7) && (6 / 3 == 2) fmt.Println(result) // 输出:true
}
In the above example code, first calculate the result of 3 plus 2 less than 7 to be true, then calculate the result of 6 divided by 3 equal to 2 to be true, and finally use the AND operator && to connect the two results to get the final result of true .
Sample code:
package main
import "fmt"
func main() {
result := 5 result += 3 fmt.Println(result) // 输出:8
}
In the above example code, 5 is first assigned to result, and then 3 is added to result, resulting in a final result of 8.
Through the introduction of this article, we have learned about the priority ordering of various operators in the Go language and provided corresponding code examples. We hope it will be helpful to everyone when using operators. When writing complex expressions, following correct precedence rules can ensure that expressions are evaluated in the correct order and avoid errors.
The above is the detailed content of Go language operator priority sorting list. For more information, please follow other related articles on the PHP Chinese website!