Many people are confused about array and slice in Go, so today we’ll talk about the differences between the two languages.

An array of

Arrays are implemented similarly in almost all computer languages: contiguous memory, as is the Go language, which implements arrays in contiguous memory. Each element has a unique index (or subscript) to access. [5]int{1:10, 2:20}

Since memory is contiguous, the CPU can easily compute the index (that is, the subscript of the array) and quickly iterate through all the elements in the array. Arrays in Go are different from arrays in C or other languages. Array variables in C are Pointers to the first element of an array. The array in Go language is a value, and the array in Go language is a value type. An array variable represents the entire array, which means that when the array in Go language is passed, the copy of the original array is passed. An array, as you might think of it in Go, is an ordered struct

slice

A slice is a small object that abstracts an array and provides related manipulation methods. Slicing has three property fields: length, capacity, and pointer to array.

In the figure above, PTR refers to a pointer to array, len refers to the length of the slice, and CAP refers to the capacity of the slice. Now, I think you have an essential understanding of arrays and slicing.

There are many ways to declare slices. What is the logical diagram for each initialization method?

fors := make([]byte, 5)ands := []byte{... }The way of

fors = s[2:4]The way of

fornilThe slice isvar s []byteThe corresponding logical diagram is

Nil slice = null slice = s := make([]byte, 0) or s := []byte{}

An empty slice pointer is not nil, whereas a nil slice pointer is nil. However, calling the built-in append(), Len, and cap functions on empty slices or nil slices has the same effect and doesn’t make any difference.

capacity

Slice is a data structure that facilitates the use and management of data sets. It can be understood as a “dynamic array”, and Slice is built around the concept of dynamic arrays. Since it’s a dynamic array, how does Slice scale up?

Keep these two rules in mind:

  • If the size of the slice is less than 1024 elements, the cap of the slice is doubled and multiplied by 2. Once the number of elements exceeds 1024, the growth factor becomes 1.25, or a quarter of the original capacity at a time.
  • If the capacity of the original array is not reached after expansion, then the position of the pointer in the slice will still be the original array. If the capacity of the original array is exceeded after expansion, then Go will create a new memory and copy the original value, which will not affect the original array at all.

Know the rules, please see the following program, try to ask the output result:

import (
    "fmt"
)
func main(a){
    array := [4]int{10.20.30.40}
    slice := array[0:2]
    newSlice := append(append(append(slice, 50), 100), 150)
    newSlice[1] + =1
    fmt.Println(slice)
}
Copy the code

Output:

20 [10]Copy the code

Is that right?

Reference: Go in Action

Blog.golang.org/go-slices-u…

For more exciting content, please follow my wechat official accountInternet Technology NestOr add wechat to discuss and exchange: