🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
> ### 例子一 * 当切片长度小于1024时, 扩容为原来的2倍, 大于或等于1024时, 扩容1.25倍 * slice\[start:end:cap\] ~~~ package main import "fmt" // make([]T, length长度, capacity容量) // 一般使用make()创建 : 用len获取切片长度, cap获取切片容量 // 一个切片在未初始化之前默认为 nil,长度为 0 // 切片是基于数组类型做的一层封装,自动扩容 // 与数组相比切片的长度是不固定的,可以追加元素,在追加时可能使切片的容量增大 func main() { var number1 []string number2 := []string{"A", "B", "C"} number3 := make([]int, 2, 2) number4 := append(number3, []int{3, 4, 5}...) //number4 := append(number3,number3...)//用省略号自动展开切片,以使用每个元素 //长度和容量 切片再切片 //cap 省略时 默认等于 len //number5... 展开number5切片成一个个元素 // var a[5]int = [5]int{5, 4, 3, 2, 1} //sort.Ints(a[:]) fmt.Println("Number1:", number1) fmt.Println("Number2:", number2) fmt.Printf("Number3: %d, len:%d, cap:%d\n", number3, len(number3), cap(number3)) fmt.Printf("Number4: %d, len:%d, cap:%d\n", number4, len(number4), cap(number4)) number5 := []string{"A", "B", "C"} number6 := []string{"D", "E"} number7 := copy(number6, number5) //等位替换原切片(返回替换成功的个数) fmt.Printf("Number6: copy success number %d --- %s\n", number7, number6) } ~~~ > ### 例子二 ~~~ package main import "fmt" func main() { //[start:end] [start:] [:end] [:] -- 第三个参数: 递增 number8 := []int{9, 12, 35, 41, 95, 60, 17, 66, 9, 10} fmt.Println("Number8-1:", number8[3:6]) //切片是引用类型(对数组的引用) fmt.Printf("Number8-2: %d, Addr:%p, Len:%d, Cap:%d \r\n", number8, number8, len(number8), cap(number8)) number9 := number8 number9[3] = 77 fmt.Printf("Number8-3: %d, Addr:%p, Len:%d, Cap:%d", number8, number8, len(number8), cap(number8)) fmt.Println("") number8 = append(number8, 11) number9[3] = 88 fmt.Printf("Number8-4: %d, Addr:%p Len:%d, Cap:%d", number8, number8, len(number8), cap(number8)) } ~~~ > ### 例子三 ~~~ package main import "fmt" func main() { number1 := make([]int, 1, 2) fmt.Printf("%p\n", number1) //图1 fmt.Printf("%p\n", &number1[0]) //图2 fmt.Printf("%p\n", &number1) //图3 切片变量的地址(ptr len cap) } ~~~ ![](/D:/%E6%AD%A3%E5%BC%8F%E6%A1%8C%E9%9D%A2/Golang%E5%B7%A5%E4%BD%9C%E7%AC%94%E8%AE%B0/book/Golang%E5%B7%A5%E4%BD%9C%E7%AC%94%E8%AE%B0/images/QQ%E6%88%AA%E5%9B%BE20180919094228.jpg) > ### 例子四 ~~~ package main import "fmt" func main() { //->■■■■■□□□□□ number1 := make([]int, 5, 10) //fmt.Println(number1[5]) //超出下标索引 //■■ ->■■■■■□□□ number2 := number1[2:7] fmt.Println(number2, cap(number2)) } ~~~ > ### 例子五 ~~~ package main import "fmt" func main() { //->■■■■■□□□□□ number1 := make([]int, 5, 10) //■■ ->■■■■■□□□ number2 := number1[2:7] number1[3] = 3 fmt.Println(number1) fmt.Println(number2) number2[3] = 4 fmt.Println(number1) fmt.Println(number2) } ~~~ > ### 例子6(去掉TODO2再看下打印结果)--(对一个变量追加元素,扔到新变量上,那么旧的变量就没用了) ~~~ package main import "fmt" func main() { x := []int{1} fmt.Println("-----TODO1-----") fmt.Printf("%v, %d, %d\n", x, len(x), cap(x)) fmt.Println("-----TODO2-----") x = append(x, 3) fmt.Printf("%v, %d, %d\n", x, len(x), cap(x)) fmt.Println("-----TODO3-----") x = append(x, 5) fmt.Printf("%v, %d, %d\n", x, len(x), cap(x)) fmt.Println("-----TODO4-----") y := append(x, 7) fmt.Printf("%v, %d, %d\n", x, len(x), cap(x)) fmt.Printf("%v, %d, %d\n", y, len(y), cap(y)) fmt.Println("-----TODO5-----") z := append(x, 9) fmt.Printf("%v, %d, %d\n", x, len(x), cap(x)) fmt.Printf("%v, %d, %d\n", z, len(z), cap(z)) fmt.Println("-----TODO6-----") fmt.Println(x, y, z) } ~~~ > ### copy ~~~ package main import "fmt" func main() { //`copy` 函数`copy`从源`slice`的`src`中复制元素到目标`dst`,并且返回复制的元素的个数 number1 := []int{1, 2, 3, 4, 5} number2 := []int{6, 7, 8} number3 := copy(number1, number2) fmt.Println(number1, number2, number3) number4 := []int{1, 2, 3, 4, 5} number5 := []int{6, 7, 8} number6 := copy(number5, number4) fmt.Println(number4, number5, number6) } ~~~ > ### append ~~~ package main import "fmt" func main() { //`append` 向`slice`里面追加一个或者多个元素,然后返回一个和`slice`一样类型的`slice` number1 := []int{1, 2, 3} fmt.Println(number1, len(number1), cap(number1)) number1 = append(number1, 4, 5) fmt.Println(number1, len(number1), cap(number1)) //按初始容量成倍扩大 number3 := append(number1, number1...) //用省略号自动展开切片,以使用每个元素 fmt.Println(number3) } ~~~