1. 基本数据类型(Primitive Data Types)
1.1 整数类型
int:平台相关,32位或64位。
int8、int16、int32、int64:固定位数的有符号整数。
uint:平台相关的无符号整数。
uint8、uint16、uint32、uint64:固定位数的无符号整数。
byte:uint8的别名,用于表示字节。
rune:int32的别名,用于表示Unicode码点。
var a int = 42
var b int8 = 127
var c uint = 255
var d byte = 'A' // 等同于 uint8
var e rune = '你' // 等同于 int32
1.2 浮点数类型
float32:32位浮点数。
float64:64位浮点数。
var f1 float32 = 3.14
var f2 float64 = 3.141592653589793
1.3 复数类型
complex64:由两个float32组成的复数。
complex128:由两个float64组成的复数。
var c1 complex64 = 1 + 2i
var c2 complex128 = 3 + 4i
1.4 布尔类型
var isTrue bool = true
var isFalse bool = false
1.5 字符串类型
var s string = "Hello, Go!"
2. 复合数据类型(Composite Data Types)
2.1 数组(Array)
var arr [3]int = [3]int{1, 2, 3}
2.2 切片(Slice)
var s []int = []int{1, 2, 3}
s = append(s, 4) // 动态添加元素
2.3 映射(Map)
var m map[string]int = map[string]int{"age": 25, "height": 180}
2.4 结构体(Struct)
type Person struct {
Name string
Age int
}
var p Person = Person{Name: "Alice", Age: 30}
2.5 指针(Pointer)
var x int = 10
var p *int = &x // p指向x的地址
3. 接口类型(Interface Type)
3.1 接口(Interface)
type Speaker interface {
Speak() string
}
type Dog struct{}
func (d Dog) Speak() string {
return "Woof!"
}
var s Speaker = Dog{}
fmt.Println(s.Speak()) // 输出: Woof!
4. 函数类型(Function Type)
4.1 函数(Function)
type MyFunc func(int) int
var f MyFunc = func(x int) int {
return x * x
}
fmt.Println(f(5)) // 输出: 25
5. 通道类型(Channel Type)
5.1 通道(Channel)
ch := make(chan int)
go func() {
ch <- 42 // 发送数据
}()
value := <-ch // 接收数据
fmt.Println(value) // 输出: 42
6. 自定义类型(Custom Type)
6.1 类型别名(Type Alias)
type MyInt int
var x MyInt = 10
6.2 结构体类型(Struct Type)
type Point struct {
X, Y int
}
var p Point = Point{X: 1, Y: 2}
1. 基本数据类型(Primitive Data Types)
1.1 整数类型
int:平台相关,32位或64位。int8、int16、int32、int64:固定位数的有符号整数。uint:平台相关的无符号整数。uint8、uint16、uint32、uint64:固定位数的无符号整数。byte:uint8的别名,用于表示字节。rune:int32的别名,用于表示Unicode码点。1.2 浮点数类型
float32:32位浮点数。float64:64位浮点数。1.3 复数类型
complex64:由两个float32组成的复数。complex128:由两个float64组成的复数。1.4 布尔类型
bool:取值为true或false。1.5 字符串类型
string:不可变的字节序列。2. 复合数据类型(Composite Data Types)
2.1 数组(Array)
2.2 切片(Slice)
2.3 映射(Map)
2.4 结构体(Struct)
2.5 指针(Pointer)
3. 接口类型(Interface Type)
3.1 接口(Interface)
4. 函数类型(Function Type)
4.1 函数(Function)
5. 通道类型(Channel Type)
5.1 通道(Channel)
6. 自定义类型(Custom Type)
6.1 类型别名(Type Alias)
6.2 结构体类型(Struct Type)