整型是所有编程语言里最基础的数据类型,Go 语言默认支持如下这些整型类型:、
在 PHP 中,默认只有 int
这一种带符号的整数类型,其长度与平台相关,在 32 位平台下其最大值为 20 亿左右(等同于 Go 语言中的 int32),64 位平台下的最大值通常是大约 9E18(等同于 Go 语言中的 int64),并且 PHP 中的整型不支持无符号类型,你可以通过 PHP_INT_MAX
常量在 PHP 中获取当前平台的最大整型值。
相比较之下,Go 支持的整型类型要丰富的多,你可以根据需要设置合适的整型类型,以节省内存空间,此外 int
和 int32
在 Go 语言里被认为是两种不同的类型(同理,int
和 int64
也是不同的类型),编译器也不会帮你自动做类型转换,比如以下的例子会有编译错误:
var int_value_1 int8
int_value_2 := 8 // int_value_2 将会被自动推导为 int 类型
int_value_1 = int_value_2 // 编译错误
注:作为弱类型的 PHP 语言,没有类似编译问题,因为变量类型都是在运行时动态决定的,在编译期根本没有类型之分。
编译错误类似于:
cannot use int_value_2 (type int) as type int8 in assignment
使用强制类型转换可以解决这个编译错误:
int_value_1 = int8(int_value_2)) // 编译通过
注:关于类型转换我们在后面介绍完所有数据类型后会单独介绍。
和 PHP 类似,Go 语言支持所有常规的整数四则运算:+
、-
、*
、/
和 %
(取余运算只能用于整数),不过由于强类型的关系,在 Go 语言中,不同类型的整型值不能直接进行算术运算,比如下面这样计算就会报编译错误:
int_value_3 := int_value_1 + int_value_2
编译错误信息如下:
invalid operation: int_value_1 + int_value_2 (mismatched types int8 and int)
类型转化之后就好了:
int_value_3 := int_value_1 + int8(int_value_2)
在 Go 语言中,支持类似 PHP 或其他语言中的自增/自减运算符,即 ++
/--
,但是只能作为语句,不能作为表达式,且只能用作后缀,不能放到变量前面:
int_value_1++ // 有效,int_value_1 的值变成 9
int_value_1 = int_value_1++ // 无效,编译报错
--int_value_1 // 无效,编译报错
也支持 +=
、-=
、*=
、/=
、%=
这种快捷写法:
int_value_1 += int_value_1 // 18
int_value_1 -= int_value_1 // 0
int_value_1 *= int_value_1 // 81
int_value_1 /= int_value_1 // 1
int_value_1 %= int_value_1 // 0
和 PHP 类似,Go 语言支持以下几种常见的比较运算符: >
、<
、==
、>=
、<=
和 !=
,比较运算符运行的结果是布尔值。
与上篇教程介绍的一样,不同类型的值不能放在一起比较,否则会报编译错处:
if int_value_1 == int_value_2 {
fmt.Println("int_value_1 和 int_value_2 相等")
}
相同类型的值才可以:
if int_value_1 < int_value_3 {
fmt.Println("int_value_1 比 int_value_3 小")
}
由此可见,所有比较运算符在比较的时候都会考虑进数据类型的因素,所以不需要类似 PHP 中 ===
和 !==
这种严格比较运算符。
不过,各种类型的整型变量都可以直接与字面常量进行比较,比如:
if int_value_1 == 8 {
fmt.Println("int_value_1 = 8")
}
位运算符以二进制的方式对数值进行运算(效率更高),和 PHP 类似,Go 语言支持以下这几种位运算符:
我们可以做一些简单的测试:
var int_value_bit uint8
int_value_bit = 255
int_value_bit = ^int_value_bit
fmt.Println(int_value_bit) // 0
int_value_bit = 1
int_value_bit = int_value_bit << 3
fmt.Println(int_value_bit) // 8
与 PHP 类似,Go 语言也支持以下逻辑运算符:
逻辑运算符计算的结果也是布尔值,通常我们可以组合使用逻辑运算符和比较运算符:
if int_value_1 < int_value_3 && int_value_1 == 8 {
fmt.Println("条件为真")
}
具体使用方式和 PHP 并没有什么区别,这里不再重复演示了。
上面介绍的 Go 语言运算符优先级如下所示(由上到下表示优先级从高到低,或者数字越大,优先级越高):
6 ^(按位取反) !
5 * / % << >> & &^
4 + - | ^(按位异或)
3 == != < <= > >=
2 &&
1 ||
注:
++
或--
只能出现在语句中,不能用于表达式,故不参与优先级判断。