Array and slice based on go language


Array based on Go language Array
is a collection of the same data type. In Go language, the array is determined from the time of declaration, and the members of the array can be modified during use. The size of the array cannot be changed.
An array is a value type. Assigning an array to another variable is equivalent to copying one copy and it becomes two arrays. The operations between the two arrays do not conflict.
Note: The array supports == and != operations, Because the array is a value type

package main

import "fmt"
func main() {
    
    
    //数组
    //存放元素的容器
    //必须指定数组存放的类型和容量
    //数组的长度是数组类型的一部分
    var ar [3]int
    fmt.Printf("%T", ar)
    //数组的初始化
    //如果我们不初始化数组,那么数组里的元素默认都是二进制的零值(bool:false  int和flaot:0  string:"")
    //初始化方式1
    ar = [3]int{
    
    1, 2, 3}
    fmt.Printf("%v\n", ar)
    //初始化方式2
    //根据初始值自动推断数组的长度
    arr := [...]int{
    
    1, 2, 3, 4, 5, 6}
    fmt.Printf("%v\n", arr)
    //初始化方式3
    //根据索引初始化
    array := [3]int{
    
    1: 1, 2: 1}
    fmt.Printf("%v\n", array)
    //遍历数组
    for i := 0; i < len(arr); i++ {
    
    
        fmt.Println(arr[i])
    }
    for i, v := range arr {
    
    
        fmt.Printf("%d--%d\n", i, v)
    }
    //多维数组
    var ar1 [3][2]int
    ar1 = [3][2]int{
    
    
        [2]int{
    
    1, 2},
        [2]int{
    
    3, 4},
        [2]int{
    
    5, 6},
    }
    fmt.Println(ar1)
    //遍历多维数组
    for _, v := range ar1 {
    
    
        for _, v1 := range v {
    
    
            fmt.Println(v1)
        }
    }

A slice
is a variable-length sequence with elements of the same data type. It is an encapsulation based on the array type. It is very flexible and supports automatic expansion. The slice is a reference type. Its content structure contains addresses. , Length and capacity, slices are used to quickly manipulate a piece of data collection.

package main

import "fmt"
func main() {
    
    
    //切片的定义
    var slice []int
    //切片的初始化
    slice = []int{
    
    1, 2, 3, 4, 5, 6, 7}
    fmt.Println(slice)
    //数组的长度和数组的容量
    fmt.Println(len(slice))
    fmt.Println(cap(slice))
    //数组的切片,得到一个切片类型
    array := [...]int{
    
    1, 2, 3, 4, 5, 6, 7, 8, 9}
    arrsl := array[0:4] //数组的切片
    //切片的容量是指底,切片的第一个元素,到底层数组的最后一个元素
    //比如,切片是[3:],那么切片的容量就是len(array)-3
    fmt.Printf("%T--%v--%d\n", arrsl, arrsl, cap(arrsl))
    //切片是引用类型,他都指向了底层的一个数组,数组发生改动,那么切片也发生改动
    array[1] = 200
    fmt.Println(arrsl)
}

Use the make() function to construct the slice.

package main

import "fmt"
func main() {
    
     
    //使用make()函数构造一个切片,参数1切片的类型,参数2切片的长度,参数3切片容量
    me := make([]int, 5, 10)
    fmt.Printf("%T--%d--%d", me, len(me), cap(me))
}

The essence of
slice A slice is a frame, which frames a continuous memory. This memory can only store values ​​of the same type.
Slices cannot be directly compared.
Slices cannot be compared. We cannot use the == operator to judge two. Whether the elements of the slice are the same, the only legal slice is more suitable for nil comparison. A nil worth slice has no underlying array. The length and capacity of a nil worth slice are both 0, but we cannot say that a length and capacity are both 0. The slice is nil, so to judge whether the slice is empty, use len(s) == 0 to judge, and should not use s == nil to judge.

package main

import "fmt"
func main() {
    
    
    me := make([]int, 0)
    fmt.Printf("%T--%d--%d", me, len(me), cap(me))
}
切片的赋值与遍历
package main

import "fmt"
func main() {
    
    
    me := make([]int, 0)
    fmt.Printf("%T--%d--%d\n", me, len(me), cap(me))
    //切片的赋值
    s1 := []int{
    
    1, 2, 3}
    s2 := s1
    s2[1] = 1000
    fmt.Printf("%v--%v\n", s1, s2)
    fmt.Println("---------")
    //切片的遍历
    for i := 0; i < len(s1); i++ {
    
    
        fmt.Println(s1[i])
    }
    fmt.Println("----------")
    for i, v := range s1 {
    
    
        fmt.Printf("%d--%d\n", i, v)
    }
}

Use append function to append elements to slices
Go language's built-in function can be append, which can dynamically add elements. Each slice will point to a bottom-level array. This array can hold a certain number of elements. When the bottom-level array cannot accommodate the new elements , The slice will be automatically expanded according to a certain expansion strategy. At this time, the underlying array pointed to by the slice will be replaced, and the expansion operation will occur when append is called.
The expansion strategy of append.
First judge, if the newly added capacity is more than twice the old capacity, the final capacity will be the newly applied capacity.
No group judgment, if the capacity of the old slice is less than 1024, then the new capacity is 2 times the old capacity.
Otherwise, if the capacity of the old slice is greater than 1042, then the new capacity is the old capacity plus 1/of the old capacity. 4
If the computing capacity overflows, the new capacity is the newly applied capacity.
Note: The expansion strategy for slices with different element types is also different

package main

import "fmt"
func main() {
    
    
    s1 := []string{
    
    "北京", "上海", "广州"}
    //切片不可以使用索引的方式添加数据,会报(索引越界错误)
    // s1[3] = "深圳"
    //在切片中追加数据
    //方式1,append的第一个参数是被添加的目标切片,第二个参数是添加的值
    s1 = append(s1, "深圳")
    fmt.Println("方式1")
    fmt.Println(s1)
    fmt.Printf("%d--%d\n", len(s1), cap(s1))
    //方式2,添加多个值
    s1 = append(s1, "围场", "承德")
    fmt.Println("方式2")
    fmt.Println(s1)
    fmt.Printf("%d--%d\n", len(s1), cap(s1))
    //方式3,在切片中,追加一个切片
    var slice1 = []string{
    
    "廊坊", "天津"}
    s1 = append(s1, slice1...)
    fmt.Println("方式2")
    fmt.Println(s1)
    fmt.Printf("%d--%d\n", len(s1), cap(s1))
    // 切片的拷贝 copy函数
    // copy函数,拷贝的切片会重新生成一个底层数组,和旧的底层数组不冲突
    // 重新定义一个切片,类型,长度都要和以前切片一致
    var ss1 = make([]string, len(s1), cap(s1))
    ss2 := s1
    copy(ss1, s1)
    fmt.Printf("%v--%d--%d\n", ss1, len(ss1), cap(ss1))
    ss2[0] = "成都"
    fmt.Printf("%v--%v--%v\n", ss1, ss2, s1)
    //在切片中,没有内置的删除函数,所以自己实现一段,实现删除功能的代码
    a1 := [...]int{
    
    1, 2, 3, 4, 5, 6, 7, 8, 9}
    slice := a1[:]
    //删除索引为2的数据
    slice = append(slice[:2], slice[3:]...)
    //因为在append函数中我们操作的是指向同一个数组的切片,所以我们就相当于在操作数组
    //我们的目标切片是数组的1,2,我们后面追加的数据,就相当于是,依次修改了,底层数组中的代码
    fmt.Printf("%v\n%v", slice, a1)
}

Guess you like

Origin blog.csdn.net/weixin_44865158/article/details/114527945