### 切片
> **slice [開始位置:結束位置]**
> len() 獲取當前切片長度(實際存在元素個數),cap()函數獲取當前切片容量(起始元素開始到底層數組中最后一個元素的個數),& 獲取地址(內存地址)
```
package main
import "fmt"
func main() {
var student = [...]string{"Tom", "Ben", "Peter"}
var student1 = student[1:2]
var student2 = student[:]
fmt.Println("student數組", student)
// student數組 [Tom Ben Peter]
fmt.Println("student1切片", student1)
//student1切片 [Ben]
fmt.Println("student2切片", student2)
// student1切片 [Tom Ben Peter]
fmt.Println("student[1]的地址", &student[1])
// student[1]的地址 0xc000076490
fmt.Println("student1[0]的地址", &student1[0])
// student1[0]的地址 0xc000076490
fmt.Println("student1切片長度", len(student1))
// student1切片長度 1
fmt.Println("student1切片容量", cap(student1))
// student1切片容量 2
}
```
### 聲明切片
> **var 切片變量名 []元素類型**
```
package main
import "fmt"
func main() {
var student []int
fmt.Println("student切片", student)
fmt.Println("student切片長度", len(student))
fmt.Println("student切片容量", cap(student))
fmt.Println("判斷student切片是否為空", student == nil)
}
結果:
student切片 []
student切片長度 0
student切片容量 0
判斷student切片是否為空 true
```
### 初始化切片
> **make([]元素類型, 切片長度, 切片容量)**
> **make([]元素類型, 長度容量一樣)**
> 容量必須大于長度
> 或者使用賦值的方法初始化
```
package main
import "fmt"
func main() {
var student []int
student = make([]int, 2, 10)
fmt.Println("student切片", student)
fmt.Println("student切片長度", len(student))
fmt.Println("student切片容量", cap(student))
fmt.Println("判斷student切片是否為空", student == nil)
}
結果:
student切片 [0 0]
student切片長度 2
student切片容量 10
判斷student切片是否為空 false
```
```
package main
import "fmt"
func main() {
var student = []int{1, 2}
fmt.Println("student切片", student)
fmt.Println("student切片長度", len(student))
fmt.Println("student切片容量", cap(student))
fmt.Println("判斷student切片是否為空", student == nil)
}
結果:
student切片 [1 2]
student切片長度 2
student切片容量 2
判斷student切片是否為空 false
```
### 為切片添加元素
> **使用append()函數添加元素,當切片長度等于容量值時(不能容納其他元素),再次使用appedn()添加元素,容量會按照2倍數擴充**
```
package main
import "fmt"
func main() {
student := make([]int, 0, 1)
fmt.Println("添加前student切片", student)
for i := 0; i < 5; i++ {
student = append(student, i)
fmt.Println("當前切片長度", len(student), "當前切片容量", cap(student))
}
fmt.Println("添加后student切片", student)
var str = [...]string{"Tom", "Ben", "Peter"}
str1 := str[0:1]
fmt.Println("str數組", str)
fmt.Println("str1切片", str1)
str1 = append(str1, "Pony") //添加的元素會覆蓋原數組位置元素
fmt.Println("添加Pony后str1切片,長度,容量", str1, len(str1), cap(str1))
fmt.Println("添加Pony后的str數組", str)
}
結果:
添加前student切片 []
當前切片長度 1 當前切片容量 1
當前切片長度 2 當前切片容量 2
當前切片長度 3 當前切片容量 4
當前切片長度 4 當前切片容量 4
當前切片長度 5 當前切片容量 8
添加后student切片 [0 1 2 3 4]
str數組 [Tom Ben Peter]
str1切片 [Tom]
添加Pony后str1切片,長度,容量 [Tom Pony] 2 3
添加Pony后的str數組 [Tom Pony Peter]
```
### 從切片中刪除元素
> **Go語言沒有刪除切片元素的方法,需要手動將刪除點前后元素連接起來**
```
package main
import "fmt"
func main() {
var student = []string{"Tom", "Ben", "Peter", "John"}
fmt.Println("刪除Ben前,長度,容量", student, len(student), cap(student))
student = append(student[0:1], student[2:]...)
//等價于 student = append(student[0:1], student[2], student[3])
fmt.Println("刪除Ben后,長度,容量", student, len(student), cap(student))
student = student[0:0]
fmt.Println("清空后,長度,容量", student, len(student), cap(student))
}
結果:
刪除Ben前,長度,容量 [Tom Ben Peter John] 4 4
刪除Ben后,長度,容量 [Tom Peter John] 3 4
清空后,長度,容量 [] 0 4
```
### 遍歷切片
> **遍歷切片的方法 類似于遍歷數組。k是索引,v是切片副本元素**
```
for k,v := range 容器變量{
fmt.Println("索引",k,"元素",v)
}
```
```
package main
import "fmt"
func main() {
var student = []string{"Tom", "Ben", "Peter", "John"}
for k, v := range student {
fmt.Println("索引", k, "元素", v)
}
}
結果:
索引 0 元素 Tom
索引 1 元素 Ben
索引 2 元素 Peter
索引 3 元素 John
```
- 安裝開發環境
- 安裝開發環境
- 安裝詳細教程
- 引入包
- Go語言基礎
- 基本變量與數據類型
- 變量
- 數據類型
- 指針
- 字符串
- 代碼總結
- 常量與運算符
- 常量
- 運算符
- 流程控制
- if判斷
- for循環
- switch分支
- goto跳轉
- 斐波那契數列
- Go語言內置容器
- 數組
- 切片
- 映射
- 函數
- 函數(上)
- 函數(中)
- 函數(下)
- 小節
- 包管理
- 結構體
- 結構體(上)
- 結構體(中)
- 結構體(下)
- 小節
- 錯誤處理
- 錯誤處理
- 宕機
- 錯誤應用
- 小節
- 文件操作
- 獲取目錄
- 創建和刪除目錄
- 文件基本操作(上)
- 文件基本操作(中)
- 文件基本操作(下)
- 處理JSON文件
- 接口與類型
- 接口的創建與實現
- 接口賦值
- 接口嵌入
- 空接口
- 類型斷言(1)
- 類型斷言(2)
- 小節
- 并發與通道
- goroutine協程
- runtime包
- 通道channel
- 單向通道channel
- select
- 線程同步
- 多線程的深入學習
- http編程
- http簡介
- Client和Request
- get請求
- post請求
- 模塊函數方法
- 模塊
- fmt庫,模塊
- 項目練習
- 爬蟲:高三網
- 爬蟲:快代理
- 爬蟲:快代理2
- 多線程:通道思路
- 多線程爬蟲:快代理