Golang basis _04- slice slice

table of Contents

@

Brief introduction

  • slice itself is not an array, it points to the bottom of the array
  • Alternatively a side length of the array, the array may be associated with partial or all of the underlying
  • It is a reference type, pointer-like
  • Can be created directly from the underlying array or gets generated, can also be used directly from the other slice assignment =
  • Use len () Get the number of elements, cap () Get Capacity
  • The make general use () to create, make ([] T, len, cap), wherein the cap can be omitted, and the case of the same value len, len denotes the number of deposit number, indicates the capacity cap
  • If len exceeds the cap, will re-allocate memory cap, then doubled
  • If a plurality of slice point to the same underlying array in which a value change will affect all

    Disclaimer assignment and initialization

    method one
func main(){
    a := [10]int{0,1,2,3,4,5,6,7,8,9}
    fmt.Println(a)
    //定义slice,[]里面不加任何东西
    var s1 []int
    s1 = a[5:10]
    //索引5到10,不包括10
    s2 := a[:5]
    //取下标从0到5,不包括5
    s3 := a[5:]
    s4 := a[5:len(a)]
    //从下标5开始,到最后一个元素,两式相同
    fmt.Println(s1)
    fmt.Println(s2)
    fmt.Println(s3)
    fmt.Println(s4)
}
/*
> Output:
command-line-arguments
[0 1 2 3 4 5 6 7 8 9]
[5 6 7 8 9]
[0 1 2 3 4]
[5 6 7 8 9]
[5 6 7 8 9]
*/

Method Two

func main(){
    s1 := make([]int, 3, 10)
    fmt.Println(len(s1),cap(s1))
}
//3,10
  • len is the current slice is the number of elements, corresponding to the CAP in the array, the start position to the last length
func main(){
    a := [9]byte{'a','b','c','d','e','f','g','h','i'}
    sa := a[2:5]
    fmt.Println("sa: ",len(sa),cap(sa),string(sa))
    sb := make([]byte,3,10)
    sb = sa[3:5]
    fmt.Println("sb: ",len(sb),cap(sb),string(sb))
}
/*
> Output:
command-line-arguments
sa:  3 7 cde
sb:  2 4 fg
*/

Write pictures described here

Resle

  • reslice is to be a slice slice
  • Index to the slice slice prevail when reslice
  • Slice index is not more than CAP slice () value
  • Overindexing will not lead to redistribution of the underlying array, but throw an error

    append function

  • Additional elements can slice the tail
  • A slice may be appended to the tail of another slice
  • If after adding the final length does not exceed the capacity added to the slice, to return to the original slice
  • Otherwise, it will re-allocate the array (cap becomes twice) copy of the original data, and
  • append increased the cap, the other elements by changing the slice, this slice will not change it, because this slice has pointed to another new array, meanwhile, changed the slice does not affect the other slice and return the original array

func main(){
    a := make([]int,3,6)
    fmt.Printf("a: %p\n",a)
    s1 := append(a,1,2,3)
    fmt.Printf("s1: %v %p %d\n",s1,s1,cap(s1))
    fmt.Printf("a: %v %p %d\n",a,a,cap(a))
    s1 = append(s1,1,2,3)
    fmt.Printf("s1: %v %p %d\n",s1,s1,cap(s1))
}
/*
> Output:
command-line-arguments
a: 0xc042072030
s1: [0 0 0 1 2 3] 0xc042072030 6
a: [0 0 0] 0xc042072030 6
s1: [0 0 0 1 2 3 1 2 3] 0xc042046060 12
*/
  • append increased the cap, the other elements by changing the slice, this slice will not change it, because this slice has pointed to another new array, meanwhile, changed the slice does not affect the other slice and return the original array
func main(){
    //全是slice
    a := []int{1,2,3,4,5}
    sa := a[1:5]
    sb := a[1:5]
    sb=append(sb,1,3,4,5,3,2,2,2)
    sb[2]=99
    fmt.Println(sb,sa)
}
/*
> Output:
command-line-arguments
[2 3 99 5 1 3 4 5 3 2 2 2] [2 3 4 5]
*/

copy function

  • copy (a, b), the inside of a copy to b
  • The copy operation is determined by the length of a shorter one of which
func main(){
    a := []int{1,2,3,4,5}
    b := []int{7,8,9}
    c := []int{11,22,333,44,55,66,77}
    copy(b,a)
    copy(c[2:4],a[1:3])
    fmt.Println(b,c)
}
/*
> Output:
command-line-arguments
[1 2 3] [11 22 2 3 55 66 77]
*/

Guess you like

Origin www.cnblogs.com/leafs99/p/golang_basic_04.html