Slice slice is a separate type in the Go language (pointing to the underlying array), unlike Python (a tool for iterating over objects), noting the distinction between arrays and slice
- Defines an empty slice format, a type that has
var s []int no length and no element assignment (as opposed to an array)
The basic operation of the array is as follows, similar to the operation of a list slice in Python, see example
package mainimport "fmt"func main() { var s1 []int //这样就是一个slice类型,既没有长度也没有元素赋值 a := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} //这是一个数组 s2 := a[5:10] //切片,索引5到10的元素,不包含索引为10 s3 := a[:3] //切片,索引1-3的元素,不包含索引3 s4 := a[6:] //切片,第6个索引到最后一个索引的元素 s5 := a[:] //切片,复制一个数组 fmt.Println(s1) fmt.Println(a) fmt.Println(s2) //数组的切片,类似python fmt.Println(s3) fmt.Println(s4) fmt.Println(s5)}/*输出s1---> []a---> [0 0 0 0 0 0 0 0 0 0]s2---> [6 7 8 9 10]s3---> [1 2 3]s4---> [7 8 9 10]s5---> [1 2 3 4 5 6 7 8 9 10]*/
Use make to initialize a slice type with three parameters
- ① Slice Type []int
- ② Initial length Len
③ Capacity Cap
package mainimport "fmt"func main() { s1 := make([]int, 3, 10) //使用make初始化一个slice,包含三个参数:①切片类型②初始长度③容量 fmt.Println(len(s1), cap(s1)) //输出长度,容量 fmt.Println(s1)}/*输出:len(s1)--->3cap(s1)--->10s1---> [0 0 0] //可变数组*/
Reslice, the original slice on the basis of slicing again, slice an array, not only take out the Len, at the same time the capacity will be the corresponding slice.
Example
//Reslice,在slice基础上再次slice.package mainimport "fmt"func main() { a := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'k', 'm'} //定义一个数组,元素为byte类型 sa := a[3:5] // slice切片,取索引三到五,不包含索引5 sb := sa[1:3] fmt.Println(string(sa)) fmt.Println(len(sa), cap(sa)) fmt.Println(string(sb)) fmt.Println(len(sb), cap(sb))}/*输出sting(sa)---> delen(sa)---> 2cap(sa)---> 6string(sb)---> eflen(sb)---> 2cap(sb)---> 5*/
Append, like the list type in Python, uses Append to append elements from the end of the array
- One slice can be appended to the other slice tail
- If the final length does not exceed the capacity appended to slice, the original slice is returned
-
If the capacity of the appended slice is exceeded, the array is reassigned and the original data is copied
package Mainimport "FMT" Func Main () {s1: = make ([]int , 3, 8)//Initialize slice, length bit 3, capacity 8 FMT. Printf ("%p\n", S1)//output S1 memory address FMT. Println (S1) S1 = append (S1, 1, 2, 3)//Add three elements at the end, and the CAP remains 8 fmt. Printf ("%p\n", S1)//original slice FMT. Println (S1) S1 = append (S1, 1, 2, 3, 4)//continues appending elements that exceed the original S1 capacity and will reallocate new memory addresses to FMT. Printf ("%p\n", S1)//cap capacity of more than 8, is 9 FMT. Println (S1)}/*p*s1---> 0xc042070040s1---> [0 0 0]p*s1s1--> 0xc042070040s1---> [0 0 0 1 2 3]p*s1 ---> 0xc04205e080//New memory address S1---> [0 0 0 1 2 3 1 2 3 4]*/
When two slice points to an array at the same time, the index of another slice index and the original array will change when one slice index is changed.
package mainimport "fmt"func main() { a := []int{1, 2, 3, 4, 5, 6} s1 := a[2:5] s2 := a[1:3] fmt.Println(s1, s2) s1[0] = 9 fmt.Println(s1, s2) fmt.Println(a)}/*s1---> [3 4 5]s2---> [2 3]//更改s1的slice索引后,s2的索引也会改s1---> [9 4 5]s2---> [2 9]a---> [1 2 9 4 5 6]*/
Copy, Format copy(s1,s2) , copy the S2 into the S1 array, and when Len (S1) > Len (S2), eventually the index in S2
Replace the index of S1, when Len (S1) <len (S2), S1 index values are all replaced by the corresponding S2 index
Example
package mainimport "fmt"func main() { s1 := []int{1, 2, 3, 4, 5} s2 := []int{6, 7, 8} copy(s1, s2) //将s2的元素拷贝到s1,s2 fmt.Println(s1) copy(s2, s1) fmt.Println(s2) copy(s1, s2[2:3]) //只拷贝某一切片的索引 fmt.Println(s1)}/*输出s1---> [6 7 8 4 5]s2---> [1 2 3]s1---> [8 2 3 4 5]*/