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.