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:"")
    ar = [3]int{
    1, 2, 3}
    fmt.Printf("%v\n", ar)
    arr := [...]int{
    1, 2, 3, 4, 5, 6}
    fmt.Printf("%v\n", arr)
    array := [3]int{
    1: 1, 2: 1}
    fmt.Printf("%v\n", array)
    for i := 0; i < len(arr); i++ {
    for i, v := range arr {
        fmt.Printf("%d--%d\n", i, v)
    var ar1 [3][2]int
    ar1 = [3][2]int{
    1, 2},
    3, 4},
    5, 6},
    for _, v := range ar1 {
        for _, v1 := range v {

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}
    array := [...]int{
    1, 2, 3, 4, 5, 6, 7, 8, 9}
    arrsl := array[0:4] //数组的切片
    fmt.Printf("%T--%v--%d\n", arrsl, arrsl, cap(arrsl))
    array[1] = 200

Use the make() function to construct the slice.

package main

import "fmt"
func main() {
    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)
    for i := 0; i < len(s1); i++ {
    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] = "深圳"
    s1 = append(s1, "深圳")
    fmt.Printf("%d--%d\n", len(s1), cap(s1))
    s1 = append(s1, "围场", "承德")
    fmt.Printf("%d--%d\n", len(s1), cap(s1))
    var slice1 = []string{
    "廊坊", "天津"}
    s1 = append(s1, slice1...)
    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[:]
    slice = append(slice[:2], slice[3:]...)
    fmt.Printf("%v\n%v", slice, a1)

Guess you like