Golang基础数据类型
数据类型类别
类型 |
描述 |
布尔型 |
布尔型的值只可以是常量 true 或者 false。一个简单的例子:var b bool = true。 |
数字类型 |
整型 int 和浮点型 float32、float64,Go 语言支持整型和浮点型数字,并且支持复数,其中位的运算采用补码。 |
字符串类型 |
字符串就是一串固定长度的字符连接起来的字符序列。Go 的字符串是由单个字节连接起来的。Go 语言的字符串的字节使用 UTF-8 编码标识 Unicode 文本。 |
派生类型 |
(a) 指针类型(Pointer) (b) 数组类型 (c) 结构化类型(struct) (d) Channel 类型 (e) 函数类型 (f) 切片类型 (g) 接口类型(interface) (h) Map 类型 |
数字类型
类型 |
描述 |
uint8 |
无符号 8 位整型 (0 到 255) |
uint16 |
无符号 16 位整型 (0 到 65535) |
uint32 |
无符号 32 位整型 (0 到 4294967295) |
uint64 |
无符号 64 位整型 (0 到 18446744073709551615) |
int8 |
有符号 8 位整型 (-128 到 127) |
int16 |
有符号 16 位整型 (-32768 到 32767) |
int32 |
有符号 32 位整型 (-2147483648 到 2147483647) |
int64 |
有符号 64 位整型 (-9223372036854775808 到 9223372036854775807) |
浮点型
类型 |
描述 |
float32 |
32位浮点型数 |
float64 |
64位浮点型数 |
complex64 |
32 位实数和虚数 |
complex128 |
64 位实数和虚数 |
其他数字类型
类型 |
描述 |
byte |
类似 uint8 |
rune |
类似 int32 |
uint |
32 或 64 位 |
int |
与 uint 一样大小 |
uintptr |
无符号整型,用于存放一个指针 |
基础用法:
类型 |
定义 |
实例 |
变量 |
var identifier type |
var a string = "Runoob" |
常量 |
const identifier [type] = value |
const WIDTH int = 5 |
数组 |
var variable_name [SIZE] variable_type |
var balance = [5]float32{1000.0, 2.0, 3.4, 7.0, 50.0} |
指针 |
var var_name *var-type |
赋值 var ip *int 取值 ip = &a |
结构体 |
type struct_variable_type struct { member definition member definition } |
type Books struct { title string author string subject string book_id int } |
切片 |
var identifier []type |
slice1 := make([]type, len) |
集合 |
map_variable := make(map[KeyType]ValueType, initialCapacity) |
m := map[string]int{ "apple": 1, "banana": 2, "orange": 3, } |