Original author, public number [programmer reading], welcome to pay attention to the public number, reprint the article please indicate the source oh.
Slice, also called Slice, is a kind of indefinite length sequence that stores a specific type, similar to array, except that array is fixed length, while Slice can be dynamically expanded, so it is much more flexible than array. Generally, []T is used to represent a Slice, where T represents the type.
In addition, arrays are value types, and when passed to a function, a copy of the array is copied. When the array is large, it consumes a lot of memory. Slices are reference types, and assigned to a function, they just copy the memory address of the slice.
Definition and use of slice
A slice, consists of three parts, the underlying array (cap), length (len), capacity, each section corresponds to an underlying array, and between multiple slices, also can share an underlying array, slice provides an access to the underlying array function, length less than capacity, the capacity can be changed dynamically, so you can think slice is a dynamic array.
Slice to create
literal
The simplest way to declare a slice, like an array, is to use a literal assignment, where the length of the slice is equal to its capacity, as shown in the following code.
Var s = int [] {1, 2, 3, 4} / / len = 4,cap= 4Copy the code
Creates slices from an array
In the example below, we’ll create an array said Monday to Sunday, can make s [I:] j, from an array or create another slice slice, by this time length is the number of the current slice element, while capacity is sliced the first element to the last element of an array of length, and the array is section corresponding to the underlying array.
In addition to s[I :j], we can write s[:j] if we start at the index 0 of the array, s[I :] if we go to the last element of the array, and S [:] if we contain the entire array.
var week = [8]string{1:"Monday", 2:"Tuesday", 3:"Wednesday", 4:"Thursday", 5:"Friday", 6:"Saturday", 7:"Sunday"} w := = week[1:]Copy the code
Use the make function to create slices
When we directly define a slice, if we do not initialize it with literal assignment, since the slice is a reference type, the initial value of the slice is nil, and an error will be reported when we use the slice. In this case, we can use the built-in function make of Go language to initialize the slice.
Var s[]int s[0] = 1 var s[]int s[0] = 1 var s[]int s[0] = 1 var s[]int s[0] = 1 var s[]int s[0] = 1 var s[]int s[0] = 1capS = make([]int,5,10)// Initialize a section with length 5 and capacity 10Copy the code
Note that a nil slice is different from an empty slice, which is not nil even though its length is both 0
Var s []int var ss = []int{} ftt.println (len(s),len(ss))//0 ftt.println (s == nil) //true
fmt.Println(ss == nil)//false
Copy the code
Length and capacity calculation
The length of the slice can be calculated using the built-in len() function, like an array, and the capacity using the built-in cap() function
package main
import "fmt"
func main() {
a := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
s := a[1:3]
fmt.Println(len(s)) //2
fmt.Println(cap(s)) //9
}
Copy the code
Your attention is the biggest encouragement on my writing road!