Golang Foundation (i)--slice usage and essence

Source: Internet
Author: User

Today's question is very basic, the study of a lot of conceptual issues, some did not answer, to do their own notes

What are value types and which are reference types in Q1:golang

There are only three types of reference in A1:golang: Slice (slice), map (dictionary), channel (pipeline);

Related knowledge: Slice usage and nature:

Turn from: https://studygolang.com/articles/5877,
Array
The slice of Go is an abstract data type on top of the array, so you have to understand the array before you know the slices.
The array type is defined by the specified and length and element types. For example
[4]int 类型表示一个四个整数的序列。数组的长度是固定的,长度是数组类型的一部分(int[4] 和 [5]int 是完全不同的类型)。
The array can be accessed as a regular index, and the expression S[n] accesses the nth element of the array.
数组不需要显式的初始化;数组元素会自动初始化为零值:

Slice

Arrays have a place for them, but arrays are not flexible enough, so arrays in go code are not used much. However, slices are used quite extensively. Slices are built on an array, but provide stronger functionality and convenience.
切片的类型是 []T,T 是切片元素的类型。Unlike arrays, slices do not have a fixed length.

The nature of the slices

A slice is a description of an array cut interval. It contains a pointer to an array, the length of the cut interval, and the capacity (the maximum length of the cut interval).

Possible "traps"

As previously mentioned, the slice operation does not replicate the underlying array. An array of this layer will be stored in memory, knowing that it is no longer referenced. Sometimes it is possible for a small memory reference to cause all of the data to be saved.

For example, the Finddigits function loads the entire file into memory, then searches for the first consecutive number, and the final result is returned as a slice.

var digitRegexp = regexp.MustCompile("[0-9]+")func FindDigits(filename string) []byte {    b, _ := ioutil.ReadFile(filename)    return digitRegexp.Find(b)}

This code behaves like an ad, and the returned []byte points to an array that holds the entire file. Because the slice references the original array, which causes the GC to not free the space of the array, a small requirement causes the entire file to be saved.
(The slice refers to the original array, and the slice operation does not affect the underlying array, which causes the GC to not release the array space later.) So fixing this problem requires copying the slices into a new slice, and the old array is freed when the method returns)
To fix the whole problem, you can copy the data you are interested in to a new slice:

func CopyDigits(filename string) []byte {    b, _ := ioutil.ReadFile(filename)    b = digitRegexp.Find(b)    c := make([]byte, len(b))    copy(c, b)    return c}

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.