Operators in go language learning

Time:2020-9-12

01 arithmetic operator

Arithmetic operators include plus sign (+), minus sign (-), multiplication sign (*), division sign (/) and remainder (%). We declare two variables A and B, and assign 10 and 5 respectively. Through code, we demonstrate the usage of arithmetic operator.

func main() {
  a := 10
  b := 5
  fmt.Println("a + b = ", a+b)
  fmt.Println("a - b = ", a-b)
  fmt.Println("a * b = ", a*b)
  fmt.Println("a / b = ", a/b)
  fmt.Println("a % b = ", a%b)
}

02 + + and–

Self increment and self decrement are not operators, they can only be used as independent statements, cannot be used in expressions, and cannot be used in front of variable names. Through the code, we demonstrate the usage of + + and -.

func main() {
  a := 10
  b := 5
  a++
  b--
  fmt.Println("a++:", a)
  fmt.Println("b--:", b)
}

03 assignment operator

In order to simplify expressions, like most languages, go also provides some assignment operators, such as equal sign (=), addition and equal (+ =), subtraction (- =), multiplication, etc(=), division (/), remainder (% =), we demonstrate the use of the assignment operator through the code.

func main() {
  aa := 50
  aa += 10
  fmt.Println("aa = ", aa)

  bb := 50
  bb -= 10
  fmt.Println("bb = ", bb)

  cc := 50
  cc *= 10
  fmt.Println("cc = ", cc)

  dd := 50
  dd /= 10
  fmt.Println("dd = ", dd)

  ff := 50
  ff %= 10
  fmt.Println("ff = ", ff)
}

04 relational operator

Relational operators are mainly used in process control statements. In go language, relational operators are greater than (>), less than (<), greater than or equal to (> =), less than or equal to (< =), equal to or equal to (= =) and not equal to (! =). Through the code, we demonstrate the usage of relational operators.

func main() {
  if a > 5 && b > 5 {
    fmt.Println (a > 5 and b > 5)
  }

  if a > 5 || b > 5 {
    fmt.Println (a > 5 or b > 5)
  }

  if a > 5 && b >= 5 {
    fmt.Println ("a > 5 and b > = 5")
  }

  if a == b {
    fmt.Println("a == b")
  }

  if a != b {
    fmt.Println("a != b")
  }

  boolean := false
  if !boolean {
    fmt.Println (both true and false)
  }
}

05 logical operator

Logic operators are also mainly used in process control statements. In go language, logical operators have and (& -), or (|), and non (!), and the code demonstration is the same as the above relational operator code.

06 & and*

Address operator (&) and pointer operator (*), not all objects can address, but variables always return address correctly. When the pointer operator is to the left of the equal sign, the target object can be updated; when the pointer operator is to the right of the equal sign, the target object can be obtained. Through the code, we demonstrate the use of address operator and pointer operator.

func main() {
  x := 50
  y := &x
  *y++
  z := *y
  fmt.Println("y = ", y)
  fmt.Println("x = ", x)
  fmt.Println("z = ", z)
}

07 bit operator

Bit operators operate on binary bits of integers in memory. Because of the space, we do not introduce bit operators too much in this paper. In our work, I suggest that bit operators should be used less or not as much as possible, because the code readability is not good. If you are interested in bit operators, you can refer to the relevant information.

08 priority

Finally, let’s introduce the priority of operators. The priority of unary operators is the highest. The priority of binary operators can be divided into five levels, from high to low

* / % << >> & &^
+ \ ^
== != <= < > >=
&&
\ \

Binary operators with the same priority are evaluated from left to right. In addition, we can use parentheses to raise the priority of operators in an expression.


Go Language Basics
Operators in go language learning