Operator analysis of go language

Time:2021-12-18

summary

Golang is a new cross platform programming language Today, Xiaobai will take you into the world of golang hand in handOperator analysis of go language

Go operator

Operators can help us perform mathematical or logical operations in programs

Operator analysis of go language

The built-in operators of go language are:

  • Arithmetic operator
  • Relational operator
  • Logical operator
  • Bitwise Operators
  • Assignment Operators

Arithmetic operator

 

operator describe
+ Add
subtract
* Multiply
/ be divided by
% Surplus
++ Self increasing
Self subtraction

 

example:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main
 
import "fmt"
 
func main() {
 
    //Define variables
    var a = 10
    var b = 2
 
    //Debug output
    fmt.println(a + b)  //Add
    fmt.println(a - b)  //Subtraction
    fmt.println(a * b)  //Multiply
    fmt.println(a / b)  //Divide
    fmt.println(a % b)  //Surplus
 
    //Self Addition & self subtraction
    a++
    b--
    fmt.println(a)  //Self addition
    fmt.println(b)  //Self subtraction
 
}

Output results:

12
8
20
5
0
11
1

Relational operator

operator describe
== Check that the two values are equal Returns true if equal, false otherwise
!= Check whether the two values are not equal. If they are not equal, return true; otherwise, return false
> greater than
< less than
>= Greater than or equal to
<= Less than or equal to

example:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main
 
import "fmt"
 
func main() {
 
    //Define variables
    var a = 10
    var b = 2
 
    //Debug output
    fmt.println(a == b)
    fmt.println(a != b)
    fmt.println(a > b)
    fmt.println(a < b)
    fmt.println(a >= b)
    fmt.println(a <= b)
 
}

Output results:

false
true
true
false
true
false

Logical operator

 

operator describe
& AND operator
| OR operator
^ XOR operator
<< Shift left operator, shift n bits left is multiplied by 2 to the power of n Fill zero in low order
>> Shift right operator, shift n bits right is divided by 2 to the nth power Low discard

 

example:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main
 
import "fmt"
 
func main() {
 
    //Define variables
    var a = 60  // 00111100
    var b = 13  // 00001101
 
    //Debug output
    fmt.println(a & b)  // 00001100: 12
    fmt.println(a | b)  // 00111101: 61
    fmt.println(a ^ b)  // 00110001: 49
    fmt.println(a << 1) // 01111000: 120
    fmt.println(a >> 1) // 00011110: 30
 
}

Output results:

12
61
49
120
30

Assignment Operators

 

operator describe
= Simple assignment
+= Add and assign
-= Subtraction before assignment
/= Divide and assign
%= Assign value after taking remainder

 

example:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main
 
import "fmt"
 
func main() {
 
    //Define variables
    var a = 10
    var b = 2
    var c int
 
    //Assignment operator
    c = a
    fmt.println(c)
 
    b += 2
    fmt.println(b)
 
    b -= 2
    fmt.println(b)
 
    a *= 2
    fmt.println(a)
 
    a /= 2
    fmt.println(a)
 
    a %= 3
    fmt.println(a)
    
}

Output results:

10
4
2
20
10
1

Operator analysis of go language

This is the end of this article about operator parsing in go language. For more information about golang operators, please search previous articles on developeppaper or continue to browse the following articles. I hope you will support developeppaper in the future!

Recommended Today

MySQL related

Indexes Bottom structure Disadvantages of hash table index: Using hash storage requires adding all files to memory, which consumes more memory space If all queries are equivalent queries, the hash is indeed fast, but in the actual scene, more data is found, and not all queries are equivalent queries, so the hash table is not […]