Golang基础知识-02-数据类型和表达式

基本类型

整型

Type Length Default Value Extra
int, unit 4 or 8 0 根据硬件决定长度,int32或int64
int8, unit8 1 0
int16, unit16 2 0
int32, unit32 4 0
int64, unit64 8 0 即便在32 位的架构上也是 64位的

这些类型相互独立,混用会报错,例:

func main() {
	var (
		a int   = 16
		b int32 = 32
	)
	
	b = a // 报错:cannot use b (type int32) as type int in assignment
	
	print(a + b) // 报错:invalid operation: a + b (mismatched types int and int32)
}

浮点型

注意:没有float类型

Type Length Default Value Extra
float32 4 0.0
float64 8 0.0 即便在32 位的架构上也是 64位的

字符串

Type Length Default Value Extra
string “” 类似使用UTF-8字符的字节数组
var (
	a string = "abc\ndef" // \n被转义
	b string = `abc\ndef` // \n被原样输出
)
print(a, b)

其他

Type Length Default Value Extra
byte 1 0 unit8的别名
array Golang中,数组是值类型
slice
map
struct
complex64 8 0+0i
complex128 8 0+0i
rune 4 0 每个数值对应Unicode字符表的一个编号,称为码值
发布了8 篇原创文章 · 获赞 0 · 访问量 52

猜你喜欢

转载自blog.csdn.net/Landercy/article/details/104100514