Home  >  Article  >  Backend Development  >  What are the operator priorities in Go language?

What are the operator priorities in Go language?

WBOY
WBOYOriginal
2023-06-10 11:04:361730browse

In the Go language, there are many kinds of operators. The calculation order of these operators is determined according to certain rules. This is the so-called operator priority, which can determine the order of program execution. This article will introduce operator precedence in Go language.

1. Basic operators

  1. Arithmetic operators

Arithmetic operators include addition ( ), subtraction (-), multiplication (*), There are five types of division (/) and remainder (%), among which the priority from high to low is:

  1. Bracket (())
  2. Negation (-x)
  3. Multiplication, division and remainder (*, /, %)
  4. Addition, subtraction (,-)

For example:

a := 10 202 // Multiplication first, then addition, which is equivalent to a := 10 (202) = 50
b := (10 20) 2 // Use Parentheses, add first, then multiply, equivalent to b := (10 20) 2 = 60

  1. Relational operators

Relational operators include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=) and less than or equal to (<=), among which the priority is from high to low For:

  1. Brackets (())
  2. Greater than, less than, greater than or equal to, less than or equal to (>, <, >=, <=)
  3. Equal, not equal (==, !=)

For example:

a := 10 > 5 && 5 < 3 || 4 > 2 // Execute && first, then ||, which is equivalent to a := (10 > 5 && 5 < 3) || (4 > 2) = true

  1. logical operator

Logical operators include negation (!), AND (&&) and OR (||), the priorities from high to low are:

  1. Brackets (())
  2. Negate (!)
  3. and (&&)
  4. or (||)

For example :

a := true || false && !true // First execute !, then &&, and finally ||, which is equivalent to a := true || false = true

2. Bitwise operators

Bitwise operators include bitwise AND (&), bitwise OR (|), XOR (^), left shift (<<) and right shift (>> ;) Five types, the priorities from high to low are:

  1. Bracket (())
  2. Left shift, right shift (<<, >> )
  3. Bitwise AND (&)
  4. Bitwise XOR (^)
  5. Bitwise OR (|)

For example :

a := 1 << 2 & 3 | 4 ^ 5 >> 2 // First execute <<, >>, then &, ^, and finally | , equivalent to a := 0 | 1 = 1

3. Assignment operator

Assignment operators include equal (=), plus equal (=), minus equal (-=) , multiplication is equal to (*=), division is equal to (/=), remainder is equal to (%=), left shift is equal to (<<=), right shift is equal to (>>=), bitwise AND is equal to ( &=), bitwise OR equal (|=) and bitwise XOR equal (^=), the priorities from low to high are:

  1. Bitwise OR equal ( |=)
  2. Bitwise XOR equals (^=)
  3. Bitwise AND equals (&=)
  4. Left shift equals (<<=)
  5. Right shift is equal to (>>=)
  6. The remainder is equal to (%=)
  7. Dividing is equal to (/=)
  8. Multiplying is equal to (*= )
  9. Minus equals (-=)
  10. Add equals (=)
  11. Equals (=)

For example:

a, b := 1, 2
a = b 3 4 // Perform multiplication first, then addition, and finally =, which is equivalent to a = a (b 3 4) = 15

By understanding the precedence of various operators in the Go language, we can write programs more accurately and better understand the calculation process of the program.

The above is the detailed content of What are the operator priorities in Go language?. 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