Home  >  Article  >  Backend Development  >  Quickly master the ordering rules of operator precedence in Go language

Quickly master the ordering rules of operator precedence in Go language

PHPz
PHPzOriginal
2023-12-23 08:58:00699browse

Quickly master the ordering rules of operator precedence in Go language

Quickly grasp the ordering rules of operator priority in Go language, you need specific code examples

In Go language, the priority of operators determines the ordering rules in expressions The order of execution of operators, which is very important to understand and write code correctly. This article will introduce the ordering rules of operator precedence in the Go language and provide specific code examples to help readers master it more quickly.

  1. Unary operators have the highest precedence. Unary operators operate on only one object, such as the positive and negative signs (, -) and logical negation (!). The following is sample code for unary operators:
package main

import "fmt"

func main() {
    a := 10
    b := -a
    c := !true

    fmt.Println(b) // 输出-10
    fmt.Println(c) // 输出false
}
  1. Multiplication, division, and modulo operators have the next highest precedence. The multiplication (*), division (/), and modulo (%) operators are executed from left to right. The sample code is as follows:
package main

import "fmt"

func main() {
    a := 10
    b := 3
    c := a * b / 2
    d := a % b

    fmt.Println(c) // 输出15
    fmt.Println(d) // 输出1
}
  1. The addition and subtraction operators have their precedence reduced again. The addition ( ) and subtraction (-) operators are executed from left to right. The following is sample code for the addition and subtraction operators:
package main

import "fmt"

func main() {
    a := 10
    b := 5
    c := a + b - 3

    fmt.Println(c) // 输出12
}
  1. The comparison operator and the equal sign operator have precedence after the addition and subtraction operators. Comparison operators (==, !=, , =) and equal sign operators (=) are executed from left to right. The following is sample code for comparison operators and equal sign operators:
package main

import "fmt"

func main() {
    a := 10
    b := 5
    c := a > b
    d := a == b

    fmt.Println(c) // 输出true
    fmt.Println(d) // 输出false
}
  1. Logical operators have the lowest precedence. The logical AND (&&) and logical OR (||) operators are executed from left to right. Here is sample code for logical operators:
package main

import "fmt"

func main() {
    a := 10
    b := 5
    c := a > b && a > 0
    d := a == b || b == 0

    fmt.Println(c) // 输出true
    fmt.Println(d) // 输出false
}

Mastering operator precedence is critical to writing correct code. The above are the ordering rules of operator precedence in Go language, and specific code examples are provided. I hope readers can get help in the process of learning and using Go language.

The above is the detailed content of Quickly master the ordering rules of operator precedence 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