GO Slice

First, the slice (Slice)

1.1 What is sliced

Go language abstract slice of the array.
Go the length of the array can not be changed, so that the set is not as useful in certain scenarios, Go provides a flexible and powerful built-in type slice ( "dynamic array"), as compared to the array slice length is not fixed , the elements may be added, so that the capacity may be increased when additional sections

Slice is a convenient, flexible and powerful wrapper. Slice does not have any data. They are just a reference to an existing array.

Compared with the array slice, no need to set the length, not the set value in [], is relatively free

From the above concepts for a slice like structure, this structure comprises three elements:

  1. Pointer to the start position in the array slice specified
  2. Length, i.e. the length of the slice
  3. The maximum length is slice start position to the last position of the array length

1.2 slices of grammar

Defined sections

var identifier []type

Slice length need not be described.
Or make () function to create a slice:

var slice1 []type = make([]type, len)
也可以简写为
slice1 := make([]type, len)
make([]T, length, capacity)

initialization

s[0] = 1
s[1] = 2
s[2] = 3
s :=[] int {1,2,3 } 
s := arr[startIndex:endIndex] 

The index created from arr startIndex to endIndex 1-element in a new slice ( front opening and closing ) and a length endIndex-startIndex

s := arr[startIndex:] 

The last element of arr default until endIndex

s := arr[:endIndex] 

The default startIndex starts from the first element of arr

package main
 
import (  
    "fmt"
)
 
func main() {  
    a := [5]int{76, 77, 78, 79, 80}
    var b []int = a[1:4] //creates a slice from a[1] to a[3]
    fmt.Println(b)
}

1.3 modifying slices

slice does not own any data. It's just a representation of the underlying array. Slice any changes made are reflected in the underlying array.

Sample code:

package main
 
import (  
    "fmt"
)
 
func main() {  
    darr := [...]int{57, 89, 90, 82, 100, 78, 67, 69, 59}
    dslice := darr[2:5]
    fmt.Println("array before",darr)
    for i := range dslice {
        dslice[i]++
    }
    fmt.Println("array after",darr) 
}

operation result:

array before [57 89 90 82 100 78 67 69 59]  
array after [57 89 91 83 101 78 67 69 59]  

When a plurality of pieces share the same underlying array, changes made to each element in the array will be reflected.

Sample code:

package main
 
import (  
    "fmt"
)
 
func main() {  
    numa := [3]int{78, 79 ,80}
    nums1 := numa[:] //creates a slice which contains all elements of the array
    nums2 := numa[:]
    fmt.Println("array before change 1",numa)
    nums1[0] = 100
    fmt.Println("array after modification to slice nums1", numa)
    nums2[1] = 101
    fmt.Println("array after modification to slice nums2", numa)
}

operation result:

array before change 1 [78 79 80]  
array after modification to slice nums1 [100 79 80]  
array after modification to slice nums2 [100 101 80]  

1.4 len () and CAP () function

The length of the slice is the number of elements in a slice. Slice capacity is the number of elements in the underlying array starting at index creation slice.

A slice is indexable, and may be acquired by len () Length
method slice provides computing capacity CAP () can be measured how many slices can be up

package main
 
import "fmt"
 
func main() {
   var numbers = make([]int,3,5)
 
   printSlice(numbers)
}
 
func printSlice(x []int){
   fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

operation result

len=3 cap=5 slice=[0 0 0]

Empty Tiles

A slice before uninitialized default to nil, a length of 0

package main
 
import "fmt"
 
func main() {
   var numbers []int
 
   printSlice(numbers)
 
   if(numbers == nil){
      fmt.Printf("切片是空的")
   }
}
 
func printSlice(x []int){
   fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

operation result

len=0 cap=0 slice=[]
切片是空的
package main
 
import "fmt"
 
func main() {
   /* 创建切片 */
   numbers := []int{0,1,2,3,4,5,6,7,8}   
   printSlice(numbers)
 
   /* 打印原始切片 */
   fmt.Println("numbers ==", numbers)
 
   /* 打印子切片从索引1(包含) 到索引4(不包含)*/
   fmt.Println("numbers[1:4] ==", numbers[1:4])
 
   /* 默认下限为 0*/
   fmt.Println("numbers[:3] ==", numbers[:3])
 
   /* 默认上限为 len(s)*/
   fmt.Println("numbers[4:] ==", numbers[4:])
 
   numbers1 := make([]int,0,5)
   printSlice(numbers1)
 
   /* 打印子切片从索引  0(包含) 到索引 2(不包含) */
   number2 := numbers[:2]
   printSlice(number2)
 
   /* 打印子切片从索引 2(包含) 到索引 5(不包含) */
   number3 := numbers[2:5]
   printSlice(number3)
 
}
 
func printSlice(x []int){
   fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

operation result

len=9 cap=9 slice=[0 1 2 3 4 5 6 7 8]
numbers == [0 1 2 3 4 5 6 7 8]
numbers[1:4] == [1 2 3]
numbers[:3] == [0 1 2]
numbers[4:] == [4 5 6 7 8]
len=0 cap=5 slice=[]
len=2 cap=9 slice=[0 1]
len=3 cap=7 slice=[2 3 4]

1.5 append () and copy () function

append additional slice to which one or more elements, and then returns a slice as a slice type
copy function copy copies elements from a source src to a destination slice in dst, and returns the number of elements copied

append function will change the contents of the array referenced by the slice, which affects the other slice refer to the same array. But no slice remaining
when I space (i.e. (cap-len) == 0) , this case will be a new array dynamically allocated space. The returned pointer points to the array slice this space, and the original
contents of the array will remain unchanged; other references to this array slice is unaffected

The following code describes a method and a copy of the copy of the slice append method adding a new element to the slice from

package main
 
import "fmt"
 
func main() {
   var numbers []int
   printSlice(numbers)
 
   /* 允许追加空切片 */
   numbers = append(numbers, 0)
   printSlice(numbers)
 
   /* 向切片添加一个元素 */
   numbers = append(numbers, 1)
   printSlice(numbers)
 
   /* 同时添加多个元素 */
   numbers = append(numbers, 2,3,4)
   printSlice(numbers)
 
   /* 创建切片 numbers1 是之前切片的两倍容量*/
   numbers1 := make([]int, len(numbers), (cap(numbers))*2)
 
   /* 拷贝 numbers 的内容到 numbers1 */
   copy(numbers1,numbers)
   printSlice(numbers1)   
}
 
func printSlice(x []int){
   fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

operation result

len=0 cap=0 slice=[]
len=1 cap=2 slice=[0]
len=2 cap=2 slice=[0 1]
len=5 cap=8 slice=[0 1 2 3 4]
len=5 cap=12 slice=[0 1 2 3 4]

Both numbers1 there is no contact with the numbers, when numbers change, numbers1 is not with the change. That copy method is not to establish contact two slices of

Guess you like

Origin www.cnblogs.com/puqunzhu/p/11700734.html