Go array slices and you do not know the difference

Opening words

  • Arrays and slices are two different data structures, more common, exist in the Go language, today we take a look at them on the use, some differences on principle?

Array

  • However, in Go, the array is a data structure having the same type of fixed size.

  • Let's take a look at the use of the array, the array type declaration when the way is [] T, in front of [] Specifies the size of the array, T specifies the type of the array, we declare the following bit array, the array size is 3, not when the initial array of the specified array default initial value is {0,0,0}

array1 := [3]int{}
//我们可以通过如下方式给数组赋值
array1[0] = 1
array1[1] = 2
array1[2] = 3
//下面这种也是数组声明的一种方式,并且初始化数组的值为{1,2}
array2 := [2]int {1,2}
  • Consider assigning array2 array1 front of us right? Remember, this approach is wrong, only in the Go language of equal size , the same type of array is an array of the same type, it can be assigned between each other, the following screenshot, we can see array2 array1 assigned to the compiler error
array2 = array1

  • See the above figure, we then think about a problem, we array3 after array2 to assign, modify the next array3 labeled value of 0 is equal to 6, the results are printed ask? The problem is that we test the array3 assigned to the array2, modify the value of array3, array2 will affect it? The answer is no, remember, in Go, belong to the basic array type, assignment between them, deliver value belongs to a copy of the same, if the array is passed as an argument between the function, also belong to value copy
第一种结果:0,0,0;  4,5; 6,5
第二种结果:0,0,0;  6,5; 6,5
  • Length of the array, we declare an array, how to get the length of the array that it? By len(array4)acquiring the array length
array4 := [2]int {1,2}
l := len(array4)
fmt.Println(""l)
  • Multidimensional array, multi-array can imagine achievement is more one-dimensional array, the following declaration of a two-dimensional array, representing a two-dimensional array, the length of each one-dimensional array of 2
array4 := [2][2]int{}
array6 := [2][2]int{{1,2},{3,4}}
  • Access to the array and iterate
//访问数组中的元素
array4 := [2]int {1,2}
//访问数组下标为0处的值并打印
fmt.Println(array4[0])
//通过range遍历数组,
//i代表数组的下标,
//v代表数组下标为i处的值
for i,v := range array4{
    fmt.Println(i,v)
}    
array5 := [2][2]int{{1,2},{3,4}}
for i,tempArray := range array5{
    //此时tempArray是一维数组
    //再通过range 遍历tempArrayy一维数组
    for j,v := range tempArray{
        fmt.Println(j,v)
    }
}

slice

  • In the Go language, the slice is an advanced use of the array, with respect to the array slice is a more convenient, flexible and efficient data structures. Slice does not store any element and only a reference (not copying values, a pointer) on the existing array

  • Slice statement following ways
  • Create a slice through the array

array1 := [3]int{1,2,3}
//将数组下标从1处到下标2处的元素转换为一个切片(前闭后开)
slice1 := array1[1:2]
  • Direct declare a slice
//下面代码直接出初始化一个切片 (这里大家有个疑问,我不管怎么看都觉得它是一个数组啊)
//记住,再go语言中,区别一个变量是数组还是切片,就看有没有定义长度
//有定义长度就是数组,如array1,没定义就是切片 如slice2
//我们也通过fmt.Println(reflect.TypeOf(array1),reflect.TypeOf(slice2))
//上面这代码打印的结果是[3]int,[]int,可以看到前者有长度,后者没有
slice2 := []int{1,2,3}
  • Create a slice through the make function, most commonly used
//[]int,指定切片的类型,3是切片的长度,6是切片的容量
slice3 := make([]int,3,6)
  • Generating a slice by slice
//声明一个切片
slice4 := []int {1,2,3,4,5,6}
//通过slice4创建一个切片,元素是slice4下标从0到1(不包含1)的元素
slice5 := slice4[0:1] 
//通过slice4创建一个切片,元素是slice4下标从0到末尾的元素
slice6 := slice4[1:] 
 //通过slice4创建一个切片,元素是slice4下标从0到3的元素
slice7 := slice4[:3]
  • Above we introduced several common sliced ​​structure, then we take a look at how slices
slice3   := make([]int,3,6)
//给切片赋值
slice3[0] = 0
slice3[1] = 1
slice3[2] = 2
//通过len([]Type) cap([]Type)两个函数查看切片的长度和容量
fmt.Println(len(slice3),cap(slice3))
结果:3,6
  • Think about the above sections we give slice3 subscript 3 assignment it? slice3[3] = 3The answer is not, although we define the length of the sections is three, the capacity is 6, but the operation is the length of the sections of the subject, if you have assigned to the maximum length, and how to do it? Slice provides us with a append([]Type, elems...Type)[]Typemethod to slice an additional element in [] represents the Type passed a slice, elems on behalf of additional elements that can pass more.
//想slice3 切片追加三个元素,返回一个新的切片
slice3 = append(slice3,3,4,5)
//此时再次查看切片的长度和容量
fmt.Println(len(slice3),cap(slice3))
结果: 6, 6 切片的长度和容量保持一致了
//思考一下,我们再次append能给切片追加元素吗? 肯定可以的,前面说过切片是可扩长的
slice3 = append(slice3,7,8,9)
//此时再次查看切片的长度和容量
fmt.Println(len(slice3),cap(slice3))
结果:9, 12
发现了什么?在切片容量满时,切片的扩容时翻倍的,也就是新的切片的容量时原切片的容量的2倍
  • Know slice of additional length, capacity, so how to remove sections of the elements in it? If you thought about when looking to create a slice by slice, you will know how to remove elements of the slice
//我们创建了一个切片,有四个元素,下标命名为0,1,2,3
slice11 := []int{1,2,3,4} 
//假设我们要删除下标为0的元素,这段代码的含义是
创建一个空的切片 slice11[:0]
创建一个从下标1到末尾元素的切片 slice11[1:]
给空切片添加元素并返回一个新的切片
slice12 := append(slice11[:0],slice11[1:]...)
//同上我们得出删除下标为i的元素的切片的公式时
sliceTemp := append(slice11[:i],slice[i+1:]...)
  • Below is a procedure to remove chips

Underlying storage array and sliced

  • We see the figure analysis

  • Based on the graph we find that the bottom of the array, the array slice is a different pointing position of the pointer in the form of slices to form a different slice, the slice itself, to modify the elements, and the array can also affect other sections. Look at the following code, I guess output
array11 := [5]int{1,2,3,4,5}
slice11 := array11[0:3]
slice11[0] = 10
sliceTemp := append(array11[:2],array11[3:]...)
slice11[0] = 11
fmt.Println(array11,slice11,sliceTemp)
//输出结果:[11 2 4 5 5] [11 2 4] [11 2 4 5]

Welcome to the concern micro-channel public number: "golang that something" more exciting look forward to your arrival

Guess you like

Origin www.cnblogs.com/sy270321/p/11388399.html