当前位置:网站首页>10 golang operator

10 golang operator

2022-07-01 06:19:00 Salted egg yolk pie


Go The language's built-in operators are :

  1. Arithmetic operator
  2. Relational operator
  3. Logical operators
  4. An operator
  5. Assignment operator

Arithmetic operator

Operator describe
+ Add up
- Subtracting the
* Multiply
/ be divided by
% Seeking remainder

Be careful :++( Self increasing ) and --( Self reduction ) stay Go Language is a separate sentence , It's not an operator .

example

package main

import "fmt"

func main() {
    
	a := 100
	b := 10
	//  When the variable is first declared , need  := ; If variables have been declared , It only needs  =  that will do 
	x := a + b
	fmt.Printf("x: %v\n", x)
	x = a - b
	fmt.Printf("x: %v\n", x)
	x = a * b
	fmt.Printf("x: %v\n", x)
	x = a / b
	fmt.Printf("x: %v\n", x)
	x = a % b
	fmt.Printf("x: %v\n", x)
	//  Variables increase and decrease by themselves 
    a++
	b--
	fmt.Printf("a: %v\n", a)
	fmt.Printf("b: %v\n", b)
}

Output results

x: 110
x: 90
x: 1000
x: 10
x: 0
a: 101
b: 9

Relational operator

Operator describe
== Check that the two values are equal , If equal returns True Otherwise return to False.
!= Check that the two values are not equal , If not equal return True Otherwise return to False.
> Check that the left value is greater than the right value , If it's a return True Otherwise return to False.
>= Check if the value on the left is greater than or equal to , If it's a return True Otherwise return to False.
< Check that the left value is less than the right value , If it's a return True Otherwise return to False.
<= Check that the left value is less than or equal to the right value , If it's a return True Otherwise return to False.

example

package main

import "fmt"

func main() {
    
	a := 1
	b := 2
	
    // Prompt the following output to refer to this  (a > b).print!  How to write it , Will automatically complete 
	fmt.Printf("(a > b): %v\n", (a > b))
	fmt.Printf("(a < b): %v\n", (a < b))
	fmt.Printf("(a == b): %v\n", (a == b))
	fmt.Printf("(a >= b): %v\n", (a >= b))
	fmt.Printf("(a <= b): %v\n", (a <= b))
	fmt.Printf("(a != b): %v\n", (a != b))
}

Output results

(a > b): false
(a < b): true
(a == b): false
(a >= b): false
(a <= b): true
(a != b): true

Logical operators

Operator describe
&& Logic AND Operator . If the operands on both sides are True, Then for True, Otherwise False.
|| Logic OR Operator . If the operands on both sides have one True, Then for True, Otherwise False.
! Logic NOT Operator . If the condition is True, Then for False, Otherwise True.

example

package main

import "fmt"

func main() {
    
	a := true
	b := false

    // Prompt the following output to refer to this  (a && b).print!  How to write it , Will automatically complete 
	fmt.Printf("(a && b): %v\n", (a && b))
	fmt.Printf("(a || b): %v\n", (a || b))
	fmt.Printf("(!a): %v\n", (!a))
	fmt.Printf("(!b): %v\n", (!b))
}

Output results

(a && b): false
(a || b): true
(!a): false
(!b): true

An operator

Bit operators operate on binary bits of integers in memory .

Operator describe
& The binary phase corresponding to each of the two numbers involved in the operation is the same as . ( Both of them are 1 Only then 1)
| The binary phase corresponding to each of the two numbers involved in the operation or . ( One of them is 1 for 1)
^ The binary of the two numbers involved in the operation is different or , When two corresponding bits are different , The result is 1. ( Two people are different 1)
<< Move left n Bits are multiplied by 2 Of n Power . “a<<b” It's a a All the binary bits of are shifted to the left b position , High level discard , Low complement 0.
>> Move right n Bits are divided by 2 Of n Power . “a>>b” It's a a All the binary bits of are shifted to the right b position .

example

package main

import "fmt"

func main() {
    
	a := 4 //  Binary system  100
	fmt.Printf("a: %b\n", a)
	b := 8 //  Binary system  1000
	fmt.Printf("b: %b\n", b)
    //  Here, in order to make everyone more clear , Binary conversion is done respectively (%b、%v)
	fmt.Printf("(a & b): %v, %b \n", (a & b), (a & b))
	fmt.Printf("(a | b): %v, %b\n", (a | b), (a | b))
	fmt.Printf("(a ^ b): %v, %b\n", (a ^ b), (a ^ b))
	fmt.Printf("(a << 2): %v, %b\n", (a << 2), (a << 2))
	fmt.Printf("(b >> 2): %v, %b\n", (b >> 2), (b >> 2))
}

Output results

a: 100
b: 1000
(a & b): 0
(a & b): 0
(a & b): 0, 0 
(a | b): 12, 1100
(a ^ b): 12, 1100
(a << 2): 16, 10000
(b >> 2): 2, 10

Assignment operator

Operator describe
= Simple assignment operators , Assign the value of an expression to an lvalue
+= Add and then assign a value
-= Subtract and then assign a value
*= Multiply and assign a value
/= Divide and assign a value
%= The value is assigned after the remainder
<<= Left shift assignment
>>= Right shift after assignment
&= Bitwise and post assignment
|= Assign value by bit or after
^= Assign a value after bitwise XOR

example

package main

import "fmt"

func main() {
    
	var a int
	a = 100
	fmt.Printf("a: %v\n", a)
	a += 1 // a = a + 1
	fmt.Printf("a: %v\n", a)
	a -= 1 // a = a -1
	fmt.Printf("a: %v\n", a)
	a *= 2 // a = a * 2
	fmt.Printf("a: %v\n", a)
	a /= 2 // a = a / 2
	fmt.Printf("a: %v\n", a)
}

Output results

a: 100
a: 101
a: 100
a: 200
a: 100
原网站

版权声明
本文为[Salted egg yolk pie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010610011015.html