当前位置:网站首页>10 golang operator
10 golang operator
2022-07-01 06:19:00 【Salted egg yolk pie】
List of articles
Go The language's built-in operators are :
- Arithmetic operator
- Relational operator
- Logical operators
- An operator
- 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
边栏推荐
- 【ManageEngine卓豪】用统一终端管理助“欧力士集团”数字化转型
- 蚂蚁新村田头村变甜头村 让厦门灌口镇田头村变甜头村的特色农产品之一是
- Essay learning record essay multi label Global
- 让田头村变甜头村的特色农产品是仙景芋还是白菜
- 扩散(多源广搜)
- Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
- 数据库er图组成要素
- Thesis learning record essay multi label lift
- HCM Beginner (IV) - time
- 地宫取宝(记忆化深搜)
猜你喜欢
Tidb database characteristics summary
机械臂速成小指南(六):步进电机驱动器
SystemVerilog learning-08-random constraints and thread control
What if the data in the cloud disk is harmonious?
Top 10 Free 3D modeling software for beginners in 2022
手把手教你实现一个深度学习框架...
【网络安全工具】USB控制软件有什么用
three.js小结
Small guide for rapid completion of mechanical arm (VI): stepping motor driver
68 cesium code datasource loading czml
随机推荐
MySQL怎么存储emoji?
Make: g++: command not found
Ant new village is one of the special agricultural products that make Tiantou village in Guankou Town, Xiamen become Tiantou village
HCM Beginner (IV) - time
SystemVerilog learning-08-random constraints and thread control
Flink实战--多流合并
【文件系统】如何在ubi之上运行squashfs
c# Xml帮助类
【企业数据安全】升级备份策略 保障企业数据安全
地宫取宝(记忆化深搜)
高阶-二叉平衡树
Top 10 Free 3D modeling software for beginners in 2022
SystemVerilog learning-09-interprocess synchronization, communication and virtual methods
地宮取寶(記憶化深搜)
SOE spatial analysis server MySQL and PostGIS geospatial database of Postgres anti injection attack
highmap gejson数据格式转换脚本
Restframework-simplejwt rewrite authentication mechanism
Dongle data collection
Understanding of C manualresetevent class
利用百度地图查询全国地铁线路