A little experience of Golang

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

Understanding New and make

New is a pointer to the return type, and make is the construct and initialize type

P: = new ([]int) fmt. Printf ("%p\n", p) P2: = Make ([]int, ten) fmt. Printf ("%p\n", p2) *p = P2

First, slice, map, and channel are reference types, where reference types are very different from C + + reference types, and C + + reference types are only supported at the compiler level, and are directly replaced by the address of the referenced object at compile time.

Slice, map and channel are the 3 types that are real, because they are reference types because they hold the address of the real object inside. This is my understanding.

To see the above code, the address of P and P2 is different, meaning that they are two slice, and when the final assignment their address is still different, indicating that they are only internal reference value copy, if you modify *p content, P2 will also be changed.

Other than that

P: = []int{}

This form of existence also proves that slice is a type, not a C + + kind of reference

So why use make is good to understand that new or directly declared Slice,map and channel is not initialized, and make will produce the actual ARRAY,MAP data and channel data, and then let Slice, The internal pointers to the map and channel point to ARRAY,MAP data and channel data.

Understanding closures

Closures are the use of variables to package functions and functions together, which sounds similar to classes. Look at the code below.

c: = 10bibao: = Func () (Func (), func ()) {i: = 0return func () {c++i++}, func () {fmt. Println (c, i)}}bibao1, Bibao2: = Bibao () bibao1 () Bibao2 () Bibao1 () Bibao2 () Bibao3, Bibao4: = Bibao () Bibao3 () Bibao4 () fmt. Println (Bibao1, Bibao2, Bibao3, Bibao4)

Print:

213 10x401140 0x401170 0x401140 0x401170

This shows that C and I are common variables in BIBAO1 and BIBAO2, which are similar to class member variables

Each resulting closure internal variable is a different instance, and Bibao3 and Bibao4 I are not shared with Bibao1 and Bibao2, and a class analogy can be understood as a new instance of each call to Bibao

Finally, Bibao1 and Bibao3,bibao2 and Bibao4 have the same function, and the function is common

The above discussion shows that if a go struct can also be used to simulate closures, what is the meaning of closures? My understanding, function Anonymous, closure is also anonymous, not named, how happy, hehe.


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.