数据类型的出现是为了把数据分成所需内存大小不同的数据,编程的时候需要用大数据的时候才需要申请大内存,这样就可以充分利用内存。
以下是 Go 中可用的基本类型:
bool (布尔)
数值类型
int8、int16、int32、int64、int
uint8、uint16、uint32、uint64、uint
float32, float64
complex64, complex128
byte
rune
string
bool
true , false
// 短类型声明
var isExit := true
// 或
var isExit bool = false
字符串类型
字符串是go中字节的集合
var say = “hello” //单行字符串
var tag = “”" // 转义符
var say = hello
//原样输出
var mLine = line1 //多行输出 line2 line3
var str = “hello, 世界”
数字类型
数字类型主要分为有符号数和无符号数,有符号数可以用来表示负数,除此之外它们还有位数的区别,不同的位数代表它们实际存储占用空间,以及取值的范围。
uint8 the set of all unsigned 8-bit integers (0 to 255)
uint16 the set of all unsigned 16-bit integers (0 to 65535)
uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
int8 the set of all signed 8-bit integers (-128 to 127)
int16 the set of all signed 16-bit integers (-32768 to 32767)
int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
float32 the set of all IEEE-754 32-bit floating-point numbers
float64 the set of all IEEE-754 64-bit floating-point numbers
complex64 the set of all complex numbers with float32 real and imaginary parts
complex128 the set of all complex numbers with float64 real and imaginary parts
类型转换
go中没有自动类型提升或转换
T(v) 是将值 v 转换为类型 T 的语法