Golang value type receiver and pointer type receiver

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

Golang value type and pointer type receiver has been confusing me, doing a few test memos here

Look at the receiver of the pointer type first:

Package Mainimport ("FMT") type basicevent struct {EventId int}func (EV *basicevent) updateeventid (id int) {ev. EventId = Id}func Main () {EV1: = &basicevent{eventid:1}fmt. Printf ("before Update id =%d\n", EV1. EVENTID) Ev1.updateeventid (2) fmt. Printf ("After update id =%d\n", EV1. EVENTID) Ev2: = Basicevent{eventid:1}fmt. Printf ("before Update id =%d\n", Ev2. EVENTID) Ev2.updateeventid (2) fmt. Printf ("After update id =%d\n", Ev2. EVENTID)}

The result of the output is:

Before update id = 1

After update id = 2

Before update id = 1

After update id = 2

This shows that the caller's internal state can be modified whenever the receiver of the method is a pointer type, regardless of whether the call is a value or a pointer. I guess this is when go automatically does a pointer conversion.


See Receiver of value type again:

Package Mainimport ("FMT") type basicevent struct {EventId int}func (EV basicevent) updateeventid (id int) {ev. EventId = Id}func Main () {EV1: = &basicevent{eventid:1}fmt. Printf ("before Update id =%d\n", EV1. EVENTID) Ev1.updateeventid (2) fmt. Printf ("After update id =%d\n", EV1. EVENTID) Ev2: = Basicevent{eventid:1}fmt. Printf ("before Update id =%d\n", Ev2. EVENTID) Ev2.updateeventid (2) fmt. Printf ("After update id =%d\n", Ev2. EVENTID)}

The result of the output is:

Before update id = 1

After update id = 1

Before update id = 1

After update id = 1

Thus, no matter whether the call is a value or a pointer, as long as the method receiver is a value type, can not change the caller's internal state.


Always visible, the main point of concern is whether receiver is a pointer or a value type when the method is defined, and if receiver is a pointer, the internal state can be changed.


In addition, questions about whether the incoming parameters can be changed

package mainimport  ("FMT") type basicevent struct {eventid int}func  Updateeventidbypointer (Ev *basicevent, newid int)  {ev. Eventid = newid}func updateeventidbyvalue (Ev basicevent, newid int)  {ev. Eventid = newid}func main ()  {ev1 := &basicevent{eventid: 1}fmt. Printf ("before update id = %d\n",  ev1. EVENTID) Updateeventidbypointer (ev1, 2) fmt. Printf ("after update id = %d\n",  ev1. EVENTID) ev2 := basicevent{eventid: 1}fmt. Printf ("before update id = %d\n",  ev2. EVENTID) Updateeventidbyvalue (ev2, 2) fmt. Printf ("after update id = %d\n",  ev2. EVENTID) ev3 := &basicevent{eventid: 1}fmt. Printf ("before update id = %d\n",  ev3. EVENTID) Updateeventidbyvalue (*ev3, 2) fmt. Printf ("after update id = %D\n ",  ev3. EVENTID) ev4 := basicevent{eventid: 1}fmt. Printf ("before update id = %d\n",  ev4. EVENTID) Updateeventidbypointer (&ev4, 2) fmt. Printf ("after update id = %d\n",  ev4. EVENTID)}

The results are as follows:

Before update id = 1

After update id = 2

Before update id = 1

After update id = 1

Before update id = 1

After update id = 1

Before update id = 1

After update id = 2

The first two are better understood. Third, it is worth noting that the pointer dereference creates another value object, at will not change the internal state. The fourth is to say, take address after get the original value object reference, so you can modify the internal state.

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.