A little pit of go pointer

Source: Internet
Author: User

It is almost certain that the go language is quoted when it refers to external variables in addition to the closure, and other times it is passed as a value. If you say formal parameters can be defined as pointers. Well, then tell you that the value of this pointer is actually used in the way of the value of the pass.

Here's a very simple example:

int ) {    +}
 - FMT. Println ("", i) stilltest (i) fmt. Println ("", i)

Output:

I-After  i  

There is no difference between the two values. But what difference does a pointer make?

*int  ) {    +}
Fmt. Println ("", i) anotherstilltest (&i) fmt. Println ("", i)

Output:

I-After  i  

You see the value of I changed, you shout this is not a reference to pass it. Man, take a closer look at the following example.

Func addressstilltest (v *int) {    x:456    = &x}
 + FMT. Println ("", X) addressstilltest (&x) fmt. Println ("", X)

Output:

X after  x  

Yes, the first method has an address in it, but we are obviously not doing any modification to the address, but rather doing a dereference operation. The value of the variable is then modified. In the above example, the operation of the address. In the function addressstilltest we try to modify the address that X points to, because the address of X is a value-transfer operation, that is, it is copied, so the modification is invalid. This is also illustrated by the final output.

Therefore, in terms of function operation, any parameters are executed in the way of the value-passing operation. Whether the pointer is worn or a general value is used for the value of the pass.

Let's look at an example of this structure below. First you need to have this:

struct {    string string}    
Func addresstest (d *Dog) {    A:= &dog{"another cute dog""  Another type"}    = A}

Output:

Dog  56another dog  56

The operation of changing the address directly to the struct is still not working. Once again, the pointer to the surface function is also a value-transfer operation.

What if you want to modify a struct?

Func anothertest (d *Dog) {    A:= &dog{"another cute dog""  Another type"}    = a.name    = A.type}

Output:

Dog  Cute dog ... Another dog  another cute dog another type

Finally, a question is explained. In c,c++ it is not right to return a pointer to a local variable from within the function. But it's possible in go. The go compiler checks to see if the function's local variable pointer will be used externally as a return value, and if so, put the variable on the heap to prolong its life cycle.

Func Test () *dog {    return &dog{"cute dog""... "}}
D: = test () fmt. Println ("", D.name, D.type)

Output:

Dog  Cute dog ...

The pit is filled!

A little pit of go pointer

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.