Home  >  Article  >  Backend Development  >  Go language operator priority sorting list

Go language operator priority sorting list

WBOY
WBOYOriginal
2023-12-23 11:43:35757browse

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.

  1. Highest precedence operator
    The highest precedence operator is parentheses (), which can be used to change the associativity and precedence of other operators.

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.

  1. Unary operator
    The unary operator includes the positive sign and the negative sign -.

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.

  1. Multiplication operator and division operator
    The multiplication operator * and the division operator / have the same precedence, and are calculated in order from left to right.

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.

  1. Addition operator and subtraction operator
    Addition operator and subtraction operator - have the same precedence and are calculated in order from left to right.

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.

  1. Relational operators
    Relational operators include less than, greater than or equal to>=, equal to== and not equal to!=, their The priority is the same.

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.

  1. Logical operators
    Logical operators include AND &&, OR || and NOT!, their precedence from high to low is NOT, AND, or.

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 .

  1. Assignment operators
    Assignment operators include the equal sign = and the compound assignment operators =, -=, *=, /=, and %=, which have the lowest priority.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn