go20/day02/array/README.md
2026-01-11 10:51:09 +08:00

53 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 数组
![alt text](image.png)
[课件](https://gitee.com/infraboard/go-course/blob/master/zh-cn/base/array.md)
## 核心概念
根据容器里面数据类型不同,直接分配分配内存(连续的内存地址空间), 内存地址有开始和结束, 访问里面的对象, 我们需要传递索引, (这个索引会根据当前数据结构int8(一个字节) ADDRADDR(+1字节))
由于是连续内存地址, 访问速度极高
+ 长度固定
+ 类型一致(静态语言)
```go
# 数组表示
[]string{}, []int{}, []bool{},
# 特殊情况
[]any{}
```
## 定义数组
![alt text](image-1.png)
1. 数组声明 + 初始化
```go
// 声明数组: [长度]type
var arr [10]int64
// 这个数组的零值是 【0,0,0,0,0,0,0,0,0,0】
// 初始化数组
arr = [10]int64{1,2,3,4,5,6,7,8,9,10}
// 简短方式(声明 + 初始化)
// := 简单声明,
arr := [10]int64{1,2,3,4,5,6,7,8,9,10}
// 动态推算大小 ... => 10, 这种方式不怎么使用
arr := [...]int64{1,2,3,4,5,6,7,8,9,10}
```
2. 赋值: 基础类型的容器,表示多个
```go
// 找到元素(数组里面的插槽), 插槽编号 就是索引(0 - 结束)
arr[0] = 1
arr[1] = 2
arr[2] = 3
```
## 子针数组(重要: 95%)