1. What is the difference between arrays and slices?
1.Array
An array is a built-in type, a collection of data of the same type. It is a value type, and element values are accessed through a subscript index starting from 0. The length is fixed after initialization and cannot be modified. When passed in as a parameter to a method, a copy of the array is copied instead of referencing the same pointer. The length of an array is also part of its type, and its length is obtained through the built-in function len(array).
2. Slice
Slices slices ("dynamic array") are also built-in types, but the length of slices is not fixed compared to arrays. Elements can be added, which may increase the capacity of slices when appending. Slices are reference types, so the same pointer will be referenced when passing slices, and modifying the value will affect other objects.
There are two concepts in slices:
- len length, length refers to the maximum subscript +1 that has been assigned a value, which can be obtained through the built-in function len().
- Cap capacity, capacity refers to the maximum number of elements that the slice can currently hold, and can be obtained through the built-in function cap().
2. Initialization of arrays and slices?
1.Array
package main import "fmt" func main() { //Initialization array initialization can be found in various forms. Check the sample code a0 := [5]int{1, 2, 3, 4, 5} //Arrays with length 5 have the element values: 1, 2, 3, 4, 5 in turn // [1 2 3 4 5] a1 := [5]int{1, 2} //Arrays with length 5 have the element values: 1, 2, 0, 0, 0. //Elements with no initial value will be assigned initial value //The default value of int is 0, the default value of string is "" //[1 2 0 0 0] a2 := [...]int{1, 2, 3, 4, 5} //Arrays with length 5, their length is determined based on the number of elements specified during initialization // [...] The length is not fixed, it changes according to the number of elements // [1 2 3 4 5] a3 := [5]int{2: 1, 3: 2, 4: 3} //Array of length 5, key:value, //key:value 2 : 1 i.e. a3[2] = 1 // 3 : 2 i.e. a3[3] = 2 // [0 0 1 2 3] a4 := [...]int{2: 1, 4: 3} //Arrays with length 5, the element values are: 0, 0, 1, 0, 3 in sequence. Since the value 3 corresponding to the maximum index 4 is specified, the length is 5 based on the initialized element and the assignment is used. // [...] The length is not fixed, it changes according to the number of elements // key:value 4 : 3 i.e. a4[4] = 3 // So there need to be 5 elements, i.e. length [5]int // [0 0 1 0 3] (a0, a1, a2, a3, a4) }
2. Slice
package main import "fmt" func main() { s0 :=[]int {1,2,3 } //[1 2 3] //Directly initialize the slice, [] means it is the slice type, and the {1,2,3} initialization values are 1,2,3 in turn, and 1,2,3 in turn. (cap(s0),len(s0)) //cap=len=3 s1 := s0[:] //[1 2 3] //Initialize slice s, which is a reference to the array arr (cap(s1),len(s1)) //cap=len=3 s2 := s0[1:2] //[2] //Create the elements in arr from the subscript startIndex to endIndex-1 as a new slice (cap(s2),len(s2)) //cap=2 len=1 s3 := s0[1:] //[2 3] //The default endIndex will represent the last element of arr until the last element of arr (cap(s3),len(s3)) //cap=len=2 s4 := s0[:2] //[1 2] //The default startIndex will indicate starting from the first element of arr (cap(s4),len(s4)) //cap=3 len=2 s5 := s4[:] //[1 2] //Initialize slice s1 by slice s (cap(s5),len(s5)) //cap=3 len=2 s6 :=make([]int,1,2) //[0] //Initialize slice s through the built-in function make(), []int is identified as a slice with its element type int (cap(s6),len(s6)) //cap=2 len=1 (s0,s1,s2,s3,s4,s5,s6) }
2. Frequently Asked Questions
1. Initialization and appendment of slices
The code is as follows (example):
package main import "fmt" func main() { // It is known from initialization, at this time len(s) = 10 cap(s) = 10 // make then at this time int already has the initial value 0 s := make([]int, 10) // Insert three elements at the end 1, 2, 3 s = append(s, 1, 2, 3) // [0 0 0 0 0 0 0 0 0 0 1 2 3] (s) }
Splicing problem
The code is as follows (example):
package main import "fmt" func main() { // Three-element slices s1 := []int{1, 2, 3} // Two-element slice s2 := []int{4, 5} // Insert two elements of s2 into s1 from the end // s2... s2 will be exported soon // s2... At this time 4, 5 s1 = append(s1, s2...) (s1) }
The difference between make
Both are memory allocation (on the heap), but make is only used for initialization of slice, map and channel (non-zero values); while new is used for type memory allocation, and the memory is set to zero. So when we write programs, we can make a good choice according to our needs.
What makes returns are the three reference types themselves; new returns a pointer to the type.
Summarize
The above two examples make it easy to understand the normal form and simple initialization of arrays and slices in Golang.
This is all about this article about arrays and slices in Golang. For more related Golang arrays and slice content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!