03 operator

Arithmetic Operator

Carry out the calculation of numerical variables, the following
Insert picture description here
sample program

func test5(){
    
    
	//
	var i int = 1
	i = i+2
	fmt.Println(i) //3

	//var i2 = i++ // 报错 unexpected ++ at end of statement
	i++
	fmt.Println(i) //4
	//++i// 报错 syntax error: unexpected ++
	//fmt.Println(i) //3
}

important point

  1. In the division operation, only the integer part is kept and the decimal part is discarded during the integer operation.
  2. Increment and decrement can only be used independently, cannot be used with assignment and other operators, and operators can only appear after the variable (not before the variable)

Assignment operator

Assign a value to the specified variable The
order of operations is from right to left, the left can only be a variable, and the right can be a variable, expression, constant value

Insert picture description here
Insert picture description here

Comparison/relational operators

Used to compare variables, the calculation result can only be true or false, as follows:
Insert picture description here
sample code

fmt.Println(1>=2) //false

Logical Operators

The final result for the plurality of operation variables bool can only be true or false
Insert picture description here
Precautions

  1. && short-circuit and if the first condition is false, the second condition is not judged and returns false directly
  2. || Short circuit or if the first condition is true, the second condition returns true without judgment

Bitwise operator

Insert picture description here

Other operators

Insert picture description here

Operator precedence

Insert picture description here

Guess you like

Origin blog.csdn.net/zhangxm_qz/article/details/114383073
03
03