Golang Xiao Kee

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Slicing and data

When learning Golang, when a slice is generated from an array, the slice actually has a pointer to that array, so the operations on the slices and arrays affect each other.

Curious is the slice is longer, when the slice exceeds the original array length, will still keep that pointer, or there is a more advanced way, today tried, it is more disappointing, to perform append operation on the slice, when the capacity of the slice is exceeded, go will assign a new array to the slice, The two parted from each other.

The append operation allocates a new array when the underlying array of slices is insufficient, so it is best to calculate the amount of space to use if you are using slices.

It may be that the immutable variable effect of functional programming has recently been felt, and it is always a bit dangerous to share the same piece of memory with an array.

There are a number of replication operations in functional languages, in performance and memory considerations, and their collection implementations should be different from normal collections and have time to see their implementations.

New and make

Go has the data structure creation functions:new and make. The distinction is a common early point of confusion but seems to quickly become. The basic distinction is this new (T) returns a *t, a pointer that Go programs can dereference implicitly (the Black Pointe RS in the diagrams), while make (T, args) returns an ordinary T, not a pointer. Often that T have inside it some implicit pointers (the gray pointers in the diagrams). New returns a pointer to zeroed memory, while make returns a complex structure.

Simply put, new returns a pointer, make returns a value, but this value usually contains pointers at the bottom.

The above English part is transferred from Http://research.swtch.com/godata

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.