What are the operator priorities in Go language?
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
- 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:
- Bracket (())
- Negation (-x)
- Multiplication, division and remainder (*, /, %)
- 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
- 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:
- Brackets (())
- Greater than, less than, greater than or equal to, less than or equal to (>, <, >=, <=)
- 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
- logical operator
Logical operators include negation (!), AND (&&) and OR (||), the priorities from high to low are:
- Brackets (())
- Negate (!)
- and (&&)
- 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:
- Bracket (())
- Left shift, right shift (<<, >> )
- Bitwise AND (&)
- Bitwise XOR (^)
- 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:
- Bitwise OR equal ( |=)
- Bitwise XOR equals (^=)
- Bitwise AND equals (&=)
- Left shift equals (<<=)
- Right shift is equal to (>>=)
- The remainder is equal to (%=)
- Dividing is equal to (/=)
- Multiplying is equal to (*= )
- Minus equals (-=)
- Add equals (=)
- 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

When using sql.Open, why doesn’t the DSN report an error? In Go language, sql.Open...
