Go language slice (Slice)

Go slicing is an abstraction of the array.

Go the length of the array can not be changed in a particular scene in such a set is not suitable, Go provides a flexible and powerful function, built-in type section ("dynamic array"), and compared the length of the array section is not fixed, may add additional elements, in may make slices increased capacity. The

declares that the Slice

slicing with the T type element is represented by []T, where T represents the type of the element in slice. The slice can be represented by a structure type inside the slice. The form is as follows:

 type slice struct {Length int Capacity int ZerothElement *byte *byte int. It can be seen from three parts: pointer, length and capacity. The pointer points to the address of the underlying array element corresponding to the first slice element. The length corresponds to the number of elements in the slice; the length can not exceed the capacity, and the capacity is generally the end of the bottom data from the beginning of the slice. The length and capacity of slice is returned by len and cap functions respectively. 

creates Slice

directly declarations to create slice

 []< element type > {element 1, element 2,... }

creates an array with 3 integral elements and returns a slice reference stored in the C.

: C = []int{6, 7, 8}


make) slice

 S1 function to create a: = make ([]int, 5) / / length and capacity are 5: S2 = make ([]int, 3, 10) / / length is 3, the capacity is 10 fmt.Println (cap (S1), S2 

) in the bottom of the array or

based on the existing slice slice or array created, such as [i:j] operator, she said to the I index, J index to the end, interception of the original array or slices, create a new section, the new section of the I index value package including the original section, but does not include J index. Note that I and j could not exceed the original slice or array index

 slice: =[]int{1,2,3,4,5}: slice1 = slice[: slice2 = slice[0:]]: slice3: slice[: 5] = fmt.Println (slice1) fmt.Println (slice2) fmt.Println (slice3) 

new slice and the original array or original slice is a common underlying so when the array, when modifications, the underlying values of the array will be changed, so the original slice values also changed.

: slice = []int{1, 2, 3, 4, 5}: newSlice = slice[1:3] newSlice[0] = 10 fmt.Println (slice) fmt.Println (newSlice) 

chip and an array of difference between

1. slice is not an array, but the array pointed to the bottom section p> < /

2. slice length is not necessarily so itself can not be compared, the array is can the.

3. slicing is an alternative to a long array of variables that can be associated to the local or all of the underlying array.

4. is a reference section (pointer address), which is an array of values (copy value)

5. slice can be created directly, references to other sections or if

6. creates an array of a plurality of slices to the underlying array of the same, in which a value changes will affect all sections of

slice of the modified

they do not have any data slicing. It is only a representation of the underlying array. Any modification to the slice will be reflected in the underlying array.

 package main import (FMT) func (main) {arr:]int{0 = [1, 2,..., 3, 4, 5, 6, 7, 8, 9}: slice = arr[2:5] fmt.Println ("array before", arr for: I = range slice) {slice[i]++} fmt.Println ("array after, ARR)}

ninth in the above procedure, we according to the array index 2,3,4 to create a slice of dslice. The for loop increases the values in these indexes one by one. When we use the for loop to print an array, we can see that the changes to the slice are reflected in the array. The output of the program is

 array before [0 12345678 9] array after [0 13455678 9]

. When multiple slices share the same underlying array, the change made by each slice will be reflected in the array.

 package main import (FMT) func (main) {array = 20, 30: [4]int{10, 40}, slice1: array[: slice2: = = array[: fmt.Println ("array before change:", array) slice1[0] = 60 fmt.Println (array after modification to slice slice1:, array slice2[1]) = 70 fmt.Println (array after modification to slice slice2:, array)}

in the 9 line, NUMA []: lack of start and end value. The default values for the start and end are 0 and Len (NUMA), respectively. Two slices nums1 and nums2 share the same array. The output of the program is

 array before change: [10 40] array after modification to slice 2030 slice1: [60 2030 40] array after modification to slice slice2: [60 7030 40]

can be clearly seen from the output section, when sharing the same array, each of the modifications will be reflected in the array. The length and capacity of the

slice, the length of the

slice, is the number of elements in the slice. The size of the slice is the number of elements in the underlying array starting from the creation of a slice index.

 package main import (FMT) func (main) {fruitarray:]string{= [... "Apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}: = fruitarray[1:3] fmt.Printf fruitslice (length of slice%d capacity%d "" len (fruitslice), cap (fruitslice)) length of is 2 and / capacity is 6}

in the above program, fruitslice is from the fruitarray index 1 and 2 created. Therefore, the length of fruitlice is 2. The length of

fruitarray is 7. Fruiteslice is created from the index 1 of fruitarray. Therefore, the capacity of fruitslice begins with the fruitarray index of 1, that is to say, from orange, the value is 6. Therefore, the capacity of fruitslice is 6. The program's output slice has a * * * length of 2 capacity of 6 * *. The

slice can reset its capacity. Any beyond this point will cause an error to be thrown when the program runs.

 package main import (FMT) func (main) {fruitarray:]string{= [... "Apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}: = fruitarray[1:3] fmt.Printf fruitslice (length of slice%d capacity%dn "" len (fruitslice), cap (fruitslice)) of is and capacity / / length 2 is 6 fruitslice = fruitslice[: cap (fruitslice)] furitslice till its capacity / / re-slicing fmt.Println ("After re-slicing length is", len (fruitslice), "and capacity is", cap (fruitslice)}

eleventh) in the program, the capacity of the fruitslice is reset. The above procedure output,

slice 2

 length of capacity 6 After re-slicing length is and capacity is 6


additional sectioning elements as we already know the length of the array is fixed, it can not increase the length of. The slice is dynamic, and the new element can be added to the slice with append. The definition of append function is

 func append (s[]T, X... T) []T

append can add elements directly to the slice tail, or add a slice to the end of another slice.

 package main import (FMT) func (main) {STR: []string{= "a", "B", "C"} fmt.Println ("strs:", "STR, length:", len (STR), "capacity:", cap (STR) = append (STR, STR) "d") fmt.Println ("strs:", "length:", STR, len (STR), "capacity:", cap (STR)}

) in the above program, the initial capacity is 3 str. In the tenth line, we add a new element to STR and assign a slice returned by append (STR, "d") to str. Now the capacity of STR has doubled, and it has become 6.

 strs: [a B c] length: 3 capacity: 3 strs: [a B C. The length and capacity of a nil slice is 0. You can use the append function to add values to the nil slice. 

 package main import (FMT) func (main) STRs []string //zero value {VaR of a slice is nil if STRs = = nil {fmt.Println ("slice is nil going to append) STRs = append (STRs," a "," B "," C "fmt.Println" string (contents:) "}

, STRs)} in the above program is names nil, we have added 3 strings to names. The output of the program is

 slice is nil going to append string contents: [a contents:. 

 package main import (FMT) func (main) {veggies: []string{= "potatoes", "tomatoes", "Brinjal"} fruits: []string{= "oranges", "apples" food}: = append (veggies, fruits...) fmt.Println (food:, food)}

tenth in the program, food through append (veggies, fruits...) to create . The output of the program is food: [potatoes tomatoes Brinjal oranges apples] .

should pay special attention to the fact that if the length of the new slice does not exceed the capacity of the source slice, it will return to the source slice. If the new slice length after the addition is longer than that of the source slice, the new slice will be returned.

 func (main) {S1 = []int{1,2,3,4,5}: fmt.Printf ("s1:%p%d%d%vn, S1, len (S1), cap (S1), S1) S2: =append (s1,6) fmt.Printf (" s3:%p%d%d%vn, S2, len (S2), cap (S2), S2 S3): s1[0:4] = fmt.Printf ("s3:%p%d%d%vn, S3, len (S3), cap (S3), S3): S4 = append (s3,6) fmt.Printf (" s4:%p%d%d%vn, S4, len (S4), cap (S4), S4 (fmt.Printf) s1:%p%d%d%vn "S1, len (S1), cap (S1), S1): S5 = append (s4,8) fmt.Printf (" s5:%p%d%d%vn, S5, len (S5), cap (S5), S5)}

pointer function



slice contains the length, volume and the zero points to an array element. When a slice is passed to a function, even if it is passed through a value, the pointer variable will refer to the same underlying array. Therefore, when a slice is passed as a parameter to a function, the changes in the function are also visible outside the function.

 package main import ("FMT") func subtactOne (numbers []int) {for I = range: numbers {numbers[i] = 2}} func {NOS (main): []int{8 = 7, 6}, fmt.Println (slice before function call, NOS) subtactOne (NOS) modifies the slice (fmt.Println / / function "slice after function call, NOS) are visible outside}

/ / modifications number

of the program in 17, calling the function to each element in the section 2. These changes are visible when a slice is printed after a function call. If you remember, this is different from the array, and the change in an array in a function is not visible outside the function.

 array before function call [8 7 6] array after function, function 5 5, multidimensional slices. 

 package main import ("FMT") func main () {pls: = [][]string {{{{"C"), "C++"}, {"}"},

This concludes the body part