当前位置:网站首页>Integers and operators in go data types (3)
Integers and operators in go data types (3)
2022-06-23 08:17:00 【weixin_ fifty-nine million two hundred and eighty-four thousand】
integer
Integer is the most basic data type in all programming languages ,Go The language supports the following integer types by default :
| type | length ( Company : byte ) | explain | Range of values | The default value is |
|---|---|---|---|---|
int8 | 1 | Signed 8 An integer | -128~127 | 0 |
uint8 | 1 | Unsigned 8 An integer , And byte Type equivalence | 0~255 | 0 |
int16 | 2 | Signed 16 An integer | -32768~32767 | 0 |
uint16 | 2 | Unsigned 16 An integer | 0~65535 | 0 |
int32 | 4 | Signed 32 An integer , And rune Type equivalence | -2147483648~2147483647 | 0 |
uint32 | 4 | Unsigned 32 An integer | 0~4294967295 | 0 |
int64 | 8 | Signed 64 An integer | -9223372036854775808~9223372036854775807 | 0 |
uint64 | 8 | Unsigned 64 An integer | 0~18446744073709551615 | 0 |
int | 32 Bit or 64 position | Platform specific | Platform specific | 0 |
uint | 32 Bit or 64 position | Platform specific | Platform specific | 0 |
uintptr | Same as corresponding pointer | Unsigned integer , Unresolved bits sufficient to store pointer values | 32 Under the platform 4 byte ,64 Under the platform 8 byte | 0 |
Go The supported integer types are very rich , You can set the appropriate integer type as needed , To save memory space , Besides int and int32 stay Go Language is considered to be two different types ( Empathy ,int and int64 It's also a different type ), The compiler won't help you with automatic type conversion , For example, the following example will have compilation errors :
var intValue1 int8
intValue2 := 8 // intValue2 Will be automatically deduced as int type
intValue1 = intValue2 // Compile error Compilation errors are similar to :
cannot use intValue2 (type int) as type int8 in assignmentThis compilation error can be solved by using cast :
intValue1 = int8(intValue2)) // Compile and pass
notes : As for type conversion, we will introduce it separately after introducing all data types .
We can also go through intValue := uint8(intValue2) This method performs both type conversion and assignment operations .
Besides , Like any programming language , You can add a prefix 0 To represent octal numbers ( Such as :077), Add prefix 0x To represent a hexadecimal number ( Such as :0xFF), And the use of E To express 10 The tandem of ( Such as :1E3 = 1000).
Operator
Arithmetic operator
Go The language supports all conventional integer four arithmetic operations :+、-、*、/ and %( The remainder operation can only be used for integers ), But because of the strong typing , stay Go In language , Different types of integer values cannot be directly arithmetically operated , For example, the following calculation will report compilation errors :
intValue3 := intValue1 + intValue2The compilation error message is as follows :
invalid operation: intValue1 + intValue2 (mismatched types int8 and int)After the type conversion :
intValue3 := intValue1 + int8(intValue2)If you are learning from a dynamic language Go, When you start writing code, you should pay particular attention to these types of problems bug.
stay Go In language , Self increment is also supported / Self - subtracting operator , namely ++/--, But only as a statement , Cannot be used as an expression , And can only be used as a suffix , Can't put in front of variables :
intValue1++ // It works ,intValue1 The value of the into 9
intValue1 = intValue1++ // Invalid , Compiler error
--intValue1 // Invalid , Compiler error And support +=、-=、*=、/=、%= This quick way of writing :
intValue1 += intValue1 // 18
intValue1 -= intValue1 // 0
intValue1 *= intValue1 // 81
intValue1 /= intValue1 // 1
intValue1 %= intValue1 // 0Comparison operator
Go The language supports the following common comparison operators : >、<、==、>=、<= and !=, The result of the comparison operator is a Boolean value .
Such as Last tutorial said ,Go It's a strongly typed language , Values of different types cannot be compared together , Otherwise, a compilation error will be reported :
if intValue1 == intValue2 {
fmt.Println("intValue1 and intValue2 equal ")
}Only values of the same type can :
if intValue1 < intValue3 {
fmt.Println("intValue1 Than intValue3 Small ")
} thus it can be seen , All comparison operators will consider the data type factor when comparing , So there is no need to introduce additional similar PHP And other dynamic languages === and !== This strict comparison operator .
however , All types of integer variables can be directly compared with literal constants , such as :
if intValue1 == 8 {
fmt.Println("intValue1 = 8")
}An operator
Bitwise operators operate on numeric values in binary form , More efficient , Better performance ,Go The following bit operators are supported by the :
| Operator | meaning | result |
|---|---|---|
x & y | Bitwise AND | hold x and y All for 1 The bit of is set to 1 |
x | y | Press bit or | hold x or y by 1 The bit of is set to 1 |
x ^ y | Bitwise XOR | hold x and y One for 1 One for 0 The bit of is set to 1 |
^x | According to the not | hold x In Chinese, it means 0 The bit of is set to 1, by 1 The bit of is set to 0 |
x << y | Move left | hold x The position in is shifted to the left y Time , Each move is equivalent to multiplying by 2 |
x >> y | Move right | hold x The position in moves to the right y Time , Each move is equivalent to dividing by 2 |
We can do some simple tests :
var intValueBit uint8
intValueBit = 255
intValueBit = ^intValueBit // According to the not
fmt.Println(intValueBit) // 0
intValueBit = 1
intValueBit = intValueBit << 3 // Move left 3 position , Equivalent to times 2^3 = 8
fmt.Println(intValueBit) // 8Logical operators
Go The language supports the following logical operators :
| Operator | meaning | result |
|---|---|---|
x && y | Logic and operators (AND) | If x and y All are true, The result is true, Otherwise, the result is false |
x || y | Logical or operator (OR) | If x or y yes true, The result is true, Otherwise, the result is false |
!x | Logical nonoperator (NOT) | If x by true, The result is false, Otherwise, the result is true |
The result of a logical operator is also a Boolean value , Usually we can combine logical operators and comparison operators :
if intValue1 < intValue3 && intValue1 == 8 {
fmt.Println(" Condition is true ")
}Operator priority
It's described above Go The precedence of language operators is as follows ( From top to bottom, the priority is from high to low , Or the larger the number , The higher the priority ):
6 ^( According to the not ) !
5 * / % << >> & &^
4 + - | ^( Bitwise XOR )
3 == != < <= > >=
2 &&
1 ||++ or -- Can only appear in statements , Cannot be used in expressions , Therefore, it does not participate in priority judgment .
边栏推荐
- Markdown学习
- Microsoft Exchange – prevent network attacks
- 正则表达式使用案例
- typeScript的介绍与变量定义的基本类型
- Vulnhub | DC: 4 | [combat]
- aquatone工具 中的2個bug修複
- Rotary table visual screening machine and its image recognition system
- 【论文笔记】Catching Both Gray and Black Swans: Open-set Supervised Anomaly Detection*
- Do not put files with garbled names into the CFS of NFS protocol
- Location of firewalld configuration file
猜你喜欢
随机推荐
List接口三个子实现类
坑爹的“敬业福”:支付宝春晚红包技术大爆发
vtk.js鼠標左鍵滑動改變窗比特和窗寬
开源技术交流丨批流一体数据同步引擎ChunJun数据还原-DDL功能模块解析
Do not put files with garbled names into the CFS of NFS protocol
INT 104_ LEC 06
Code quality level 3 - readable code
Create an orderly sequence table and perform the following operations: 1 Insert element x into the table and keep it in order; 2. find the element with the value of X, and delete it if found; 3. outpu
QT irregular shape antialiasing
GTEST death test
Multi Chain and cross chain are the future
Location of firewalld configuration file
Use of tensorboard
C restart application
华为云服务器弹性公网IP无法ping
【Try to Hack】ip地址
PHP serialization and deserialization CTF
jmeter压测结果分析
Idea true permanent activation method and permanent activation code tutorial
开源软件、自由软件、Copyleft、CC都是啥,傻傻分不清楚?









