当前位置:网站首页>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
边栏推荐
- SystemVerilog learning-09-interprocess synchronization, communication and virtual methods
- Oracle sequence + trigger
- 【ManageEngine卓豪】移动终端管理解决方案,助力中州航空产业数字化转型
- Cjc8988 Low Power Stereo codec with 2 stereo headphone drivers
- Pit of kotlin bit operation (bytes[i] and 0xff error)
- Factorial divisor (unique decomposition theorem)
- Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village
- highmap gejson数据格式转换脚本
- Transformer le village de tiantou en un village de betteraves sucrières
- JMM详解
猜你喜欢

让厦门灌口镇田头村变甜头村的特色农产品之一是蚂蚁新村

Pla ne colle pas sur le lit: 6 solutions simples

three. JS summary

OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
![Pit of kotlin bit operation (bytes[i] and 0xff error)](/img/2c/de0608c29d8af558f6f8dab4eb7fd8.png)
Pit of kotlin bit operation (bytes[i] and 0xff error)

Uniapp tree level selector
![kotlin位运算的坑(bytes[i] and 0xff 报错)](/img/2c/de0608c29d8af558f6f8dab4eb7fd8.png)
kotlin位运算的坑(bytes[i] and 0xff 报错)

分布式锁实现

Ant new village is one of the special agricultural products that make Tiantou village in Guankou Town, Xiamen become Tiantou village

Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
随机推荐
MongoDB:一、MongoDB是什么?MongoDB的优缺点
OpenGL es: (4) detailed explanation of EGL API (Continued)
Golang panic recover custom exception handling
Skywalking integrated Nacos dynamic configuration
kotlin位运算的坑(bytes[i] and 0xff 报错)
ABP 学习解决方案中的项目以及依赖关系
Linux closes the redis process SYSTEMd+
Oracle sequence + trigger
ForkJoin和Stream流测试
Multi label lsml for essay learning records
Servlet
Distributed lock implementation
DHT11 temperature and humidity sensor
HCM Beginner (IV) - time
68 Cesium代码datasource加载czml
MySQL怎么存储emoji?
PLA不粘貼在床上:6個簡單的解决方案
C# ManualResetEvent 类的理解
【企业数据安全】升级备份策略 保障企业数据安全
让厦门灌口镇田头村变“甜头”村的特色农产品之一是