Fast understanding of the internal implementation principles of go arrays and slices

Source: Internet
Author: User

Many people on the go language array and slice silly Division is unclear, today we start from the bottom, to talk about what the difference between the two.

Array

Almost all computer languages, array implementations are similar: a continuous memory, the go language is the same, the go language array of the underlying implementation is a contiguous memory space. Each element has a unique index (or called 下标 ) to access. As shown, is the [5]int{1:10, 2:20} internal implementation logic diagram of the array:

Because memory is contiguous, the CPU can easily compute the index (that is, the array 下标 ), which allows you to quickly iterate over all the elements in the algebraic group.
The go language array is different from the C language or other language array, the C language array variable is a pointer to the first element of the array, and the go Language array is a value, the Go language array is a value type, an array variable represents the entire array, meaning that the Go language array is passed, A copy of the original array is passed. You can understand that an array of go languages is an orderlystruct

Slice

A slice is a small object that is abstracted from an array, and provides an associated method of action. Slices have three attribute fields: length, capacity, and pointers to arrays.

, refers to the pointer of the array, refers to the length of the ptr len Slice, refers to the capacity of the cap slice. Now, I think you have an essential understanding of the array and the slices.

There are several ways to declare slices, what is the logical diagram for each initialization method?

For s := make([]byte, 5) and s := []byte{...} the way

For s = s[2:4] the way

For nil a slice that var s []byte corresponds to a logical diagram is

Here is a description: slices nil and slices are not the same, empty slices s := make([]byte, 0) or s := []byte{} out of slices
The logical diagram for an empty slice is:

The empty slice pointer is not nil, and the nil slice pointer is nil. However, whether it is an empty slice or a nil slice, it calls the built-in function append() , len and cap the effect is the same, feeling no difference.

Expansion

Slice this data structure makes it easy to use and manage the collection, which can be understood as a "dynamic array" and is slice built around the concept of dynamic arrays. Since it is a dynamic array, how does the slice scale?

Keep in mind the following two rules:

    • If the size of the slice is less than 1024 elements, then the slice cap doubles at the time of expansion, multiplied by 2, and once the number of elements exceeds 1024 elements, the growth factor becomes 1.25, which increases the original capacity by One-fourth at a time.
    • If the capacity of the original array has not been touched after the expansion, then the position of the pointer in the slice is still the original array, and if the capacity of the original array is exceeded after the expansion, then go will open a new memory and copy the original value, which will not affect the original array.

Know the rules, please look at the following procedure, how to output the result:

import (    "fmt")func main(){    [4]int{10, 20, 30, 40}    slice := array[0:2]    append(append(append(slice, 50), 100), 150)    newSlice[1] += 1    fmt.Println(slice)}

Output:

[1020]

Are you correct?

Reference documents:
"Go in Action"
Https://blog.golang.org/go-slices-usage-and-internals

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.