Arrays and slices of go

What is an array?

array

An array is a sequence of fixed length elements of a specific type. An array can be composed of zero or more elements

How to define an array?

One way

package main
import "fmt"

func arraytest()  {
    var x [3] int
    fmt.Println(x) 
}
// Output [0 0 0]
func main()  {
    arraytest()
    }

Using quick declaration arrays

x3 :=[3] int {112,78}
fmt.Println(x3) 
//Output [112 78 0]

//Getting array value

var x2[3] int
    x2[0] = 1 //First element
    x2[1] = 11
    x2[2] = 22 
    fmt.Println(x2) 
//Output [11 22]

//Using the for loop to get the data of an array

 x6 :=[...] int {3,5,6,82,34}
    for i :=0; i< len(x6);i++{
        fmt.Println(i,x6[i])
    }
    // Output the following 0 for index number and 3 for specific value
 0 3
 1 5
 2 6
 3 82
 4 34

//Get by using range in go

for i,v := range x6{
fmt.Printf("%d of x6 is %d\n",i,v)
}
// output 
0 3
1 5
2 6
3 82
4 34

//Access elements only

for _,v1 :=range x6 {
    fmt.Printf("x6 array value is %d\n",v1)
}
// output

0 of x6 is 3
1 of x6 is 5
2 of x6 is 6
3 of x6 is 82
4 of x6 is 34

Section

What is slice?

Slice is a special data structure in Golang, which is more convenient to use and manage data sets

//Definition of slice

package main
import "fmt"

func slicetest() {
a1 :=[4] int {1,3,7,22}
fmt.Println(a1)

// output

[1 3 7 22]
var b1 []int = a1[1:4]
fmt.Println(b1,len(b1)) 

// output

[3 7 22] 3

}

func main() {
slicetest()
}

//Declare slices with make
When making an initialization function slice, if you do not specify its capacity, it will be the same as the length. If you specify its capacity when initializing

 s1 :=make([]int, 5) 
    fmt.Printf("The length of s1: %d\n",len(s1)) # length
    fmt.Printf("The capacity of; s1: %d\n",cap(s1)) # cap capacity
    fmt.Printf("The value of s1:%d\n",s1)
    // output 

    The length of s1: 5
The capacity of; s1: 5
The value of s1:[0 0 0 0 0]

    s12 :=make([]int, 5,8) # Specify a length of 5 and a capacity of 8
    fmt.Printf("The length of s12: %d\n",len(s12))
    fmt.Printf("The capacity of; s12: %d\n",cap(s12))
    fmt.Printf("The value of s12:%d\n",s12)
    // output

    The length of s12: 5
The capacity of; s12: 8
The value of s12:[0 0 0 0 0]

// 
s3 := []int{1, 2, 3, 4, 5, 6, 7, 8}
s4 := s3[3:6]
fmt.Printf("The length of s4: %d\n", len(s4))
fmt.Printf("The capacity of s4: %d\n", cap(s4))
fmt.Printf("The value of s4: %d\n", s4)

// output

The length of s4: 3  # The length is 3. 
The capacity of s4: 5 # Capacity is 5.  
The value of s4: [4 5 6]  

// The relationship between slice and array

//The capacity of slices can be seen as the number of elements in the underlying array
s4 It is through s3 Slice it, so s3 The underlying array of s4 We can't extend the slice to the left, so s4 Can not see s3 The three leftmost elements, so s4 Capacity of 5

//Slice acquisition

before_slice :=[] int {1,4,5,23,13,313,63,23}
after_slice := before_slice[3:7]

fmt.Println("array before change",before_slice)

// Traverse with for range

for i := range after_slice{
    after_slice[i]++
}
fmt.Println("after cahnge",before_slice)

/ / output

array before change [1 4 5 23 13 313 63 23]
after cahnge [1 4 5 24 14 314 64 23]

The difference between array and slice

Is the capacity scalable? Array capacity is not scalable, slice can

Is it possible to compare? //Arrays of the same length can be compared

Keywords: Go

Added by chris1 on Tue, 07 Jan 2020 17:14:01 +0200