当前位置:网站首页>Go 数据类型篇(三)之整型及运算符
Go 数据类型篇(三)之整型及运算符
2022-06-23 07:49:00 【weixin_59284282】
整型
整型是所有编程语言里最基础的数据类型,Go 语言默认支持如下这些整型类型:
| 类型 | 长度(单位:字节) | 说明 | 值范围 | 默认值 |
|---|---|---|---|---|
int8 | 1 | 带符号8位整型 | -128~127 | 0 |
uint8 | 1 | 无符号8位整型,与 byte类型等价 | 0~255 | 0 |
int16 | 2 | 带符号16位整型 | -32768~32767 | 0 |
uint16 | 2 | 无符号16位整型 | 0~65535 | 0 |
int32 | 4 | 带符号32位整型,与 rune类型等价 | -2147483648~2147483647 | 0 |
uint32 | 4 | 无符号32位整型 | 0~4294967295 | 0 |
int64 | 8 | 带符号64位整型 | -9223372036854775808~9223372036854775807 | 0 |
uint64 | 8 | 无符号64位整型 | 0~18446744073709551615 | 0 |
int | 32位或64位 | 与具体平台相关 | 与具体平台相关 | 0 |
uint | 32位或64位 | 与具体平台相关 | 与具体平台相关 | 0 |
uintptr | 与对应指针相同 | 无符号整型,足以存储指针值的未解释位 | 32位平台下为4字节,64位平台下为8字节 | 0 |
Go 支持的整型类型非常丰富,你可以根据需要设置合适的整型类型,以节省内存空间,此外 int 和 int32 在 Go 语言里被认为是两种不同的类型(同理,int 和 int64 也是不同的类型),编译器也不会帮你自动做类型转换,比如以下的例子会有编译错误:
var intValue1 int8
intValue2 := 8 // intValue2 将会被自动推导为 int 类型
intValue1 = intValue2 // 编译错误编译错误类似于:
cannot use intValue2 (type int) as type int8 in assignment使用强制类型转换可以解决这个编译错误:
intValue1 = int8(intValue2)) // 编译通过
注:关于类型转换我们在后面介绍完所有数据类型后会单独介绍。
我们还可以通过 intValue := uint8(intValue2) 这种方式同时完成类型转化和赋值操作。
此外,和其他编程语言一样,可以通过增加前缀 0 来表示八进制数(如:077),增加前缀 0x 来表示十六进制数(如:0xFF),以及使用 E 来表示 10 的连乘(如:1E3 = 1000)。
运算符
算术运算符
Go 语言支持所有常规的整数四则运算:+、-、*、/ 和 %(取余运算只能用于整数),不过由于强类型的关系,在 Go 语言中,不同类型的整型值不能直接进行算术运算,比如下面这样计算就会报编译错误:
intValue3 := intValue1 + intValue2编译错误信息如下:
invalid operation: intValue1 + intValue2 (mismatched types int8 and int)类型转化之后就好了:
intValue3 := intValue1 + int8(intValue2)如果你是从动态语言转过来学习 Go,在刚开始写代码时尤其要注意这些因为类型问题产生的 bug。
在 Go 语言中,也支持自增/自减运算符,即 ++/--,但是只能作为语句,不能作为表达式,且只能用作后缀,不能放到变量前面:
intValue1++ // 有效,intValue1 的值变成 9
intValue1 = intValue1++ // 无效,编译报错
--intValue1 // 无效,编译报错还支持 +=、-=、*=、/=、%= 这种快捷写法:
intValue1 += intValue1 // 18
intValue1 -= intValue1 // 0
intValue1 *= intValue1 // 81
intValue1 /= intValue1 // 1
intValue1 %= intValue1 // 0比较运算符
Go 语言支持以下几种常见的比较运算符: >、<、==、>=、<= 和 !=,比较运算符运行的结果是布尔值。
如上篇教程所说,Go 是强类型语言,不同类型的值不能放在一起比较,否则会报编译错处:
if intValue1 == intValue2 {
fmt.Println("intValue1 和 intValue2 相等")
}相同类型的值才可以:
if intValue1 < intValue3 {
fmt.Println("intValue1 比 intValue3 小")
}由此可见,所有比较运算符在比较的时候都会考虑进数据类型的因素,所以不需要额外引入类似 PHP 等动态语言中的 === 和 !== 这种严格比较运算符。
不过,各种类型的整型变量都可以直接与字面常量进行比较,比如:
if intValue1 == 8 {
fmt.Println("intValue1 = 8")
}位运算符
位运算符以二进制的方式对数值进行运算,效率更高,性能更好,Go 语言支持以下这几种位运算符:
| 运算符 | 含义 | 结果 |
|---|---|---|
x & y | 按位与 | 把 x 和 y 都为 1 的位设为 1 |
x | y | 按位或 | 把 x 或 y 为 1 的位设为 1 |
x ^ y | 按位异或 | 把 x 和 y 一个为 1 一个为 0 的位设为 1 |
^x | 按位取反 | 把 x 中为 0 的位设为 1,为 1 的位设为 0 |
x << y | 左移 | 把 x 中的位向左移动 y 次,每次移动相当于乘以 2 |
x >> y | 右移 | 把 x 中的位向右移动 y 次,每次移动相当于除以 2 |
我们可以做一些简单的测试:
var intValueBit uint8
intValueBit = 255
intValueBit = ^intValueBit // 按位取反
fmt.Println(intValueBit) // 0
intValueBit = 1
intValueBit = intValueBit << 3 // 左移 3 位,相当于乘以 2^3 = 8
fmt.Println(intValueBit) // 8逻辑运算符
Go 语言支持以下逻辑运算符:
| 运算符 | 含义 | 结果 |
|---|---|---|
x && y | 逻辑与运算符(AND) | 如果 x 和 y 都是 true,则结果为 true,否则结果为 false |
x || y | 逻辑或运算符(OR) | 如果 x 或 y 是 true,则结果为 true,否则结果为 false |
!x | 逻辑非运算符(NOT) | 如果 x 为 true,则结果为 false,否则结果为 true |
逻辑运算符计算的结果也是布尔值,通常我们可以组合使用逻辑运算符和比较运算符:
if intValue1 < intValue3 && intValue1 == 8 {
fmt.Println("条件为真")
}运算符优先级
上面介绍的 Go 语言运算符优先级如下所示(由上到下表示优先级从高到低,或者数字越大,优先级越高):
6 ^(按位取反) !
5 * / % << >> & &^
4 + - | ^(按位异或)
3 == != < <= > >=
2 &&
1 ||++ 或 -- 只能出现在语句中,不能用于表达式,故不参与优先级判断。
边栏推荐
- Regular expression use cases
- After reading five books, I summarized these theories of wealth freedom
- 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
- Image segmentation - improved network structure
- 开源软件、自由软件、Copyleft、CC都是啥,傻傻分不清楚?
- MFC radio button grouping
- 建立一有序的顺序表,并实现下列操作: 1.把元素x插入表中并保持有序; 2.查找值为x的元素,若找到将其删除; 3.输出表中各元素的值。
- Go language basic conditional statement if
- openvino系列 19. OpenVINO 与 PaddleOCR 实现视频实时OCR处理
- Acwing第 56 场周赛【完结】
猜你喜欢
![Match 56 de la semaine d'acwing [terminé]](/img/f6/cd650331c819a27f17c9ce6cd0c569.png)
Match 56 de la semaine d'acwing [terminé]

Openvino series 19 Openvino and paddleocr for real-time video OCR processing

Apache Solr arbitrary file read replication

船长阿布的灵魂拷问

开源软件、自由软件、Copyleft、CC都是啥,傻傻分不清楚?

Gif verification code analysis

A record of "from scratch" in college student accounts

Does huangrong really exist?

Image segmentation - improved network structure

Check the file through the port
随机推荐
VTK. Le bouton gauche de la souris JS glisse pour changer le niveau et la largeur de la fenêtre
C# richTextBox控制最大行数
Moodle e-learning platform fixes the session hijacking error that leads to pre authorized rce
深度学习------不同方法实现vgg16
Talk about routing design in service governance
Vulnhub | DC: 3 |【实战】
生产环境服务器环境搭建+项目发布流程
Kwai 350014
华为云服务器弹性公网IP无法ping
google常用语法
@Controller和@RestController的区别?
顺序表课设
MySQL slow query record
Implementation principle and source code analysis of ThreadPoolExecutor thread pool
ArcMap batch delete points closer
Using jetpack datastore for data storage
AVL树的实现
2022 final examination of software project management of School of software, Shandong University (recall version)
socket编程(多进程)
On ThreadLocal and inheritablethreadlocal, source code analysis