" The pointer to function assignment captures the current receiver." I quite agree with your above conclusion, and the test results also prove it. That seems to be the way it's designed right now, but what I find a little hard to understand here is why it's not designed as "The pointer to function assignment captures the current variable state."
In my opinion, if the function assignment captures the current variable, not the receiver, it seems more acceptable. I wrote an example to demo it through closure as the following: /*** type Zoo struct { Animal string } func (z Zoo) Display(){ fmt.Printf("Current animal is:%s\n", z.Animal) } func ZooDisplay(cur *Zoo) func() { return func(){ cur.Display() } } func main(){ gz := &Zoo{ Animal: "Monkey", } pf := gz.Display pf2 := ZooDisplay(gz) pf() //display "Current animal is Monkey" pf2() //display "Current animal is Monkey" gz.Display() //display "Current animal is Monkey" gz.Animal="Tiger" pf() //display "Current animal is Monkey" pf2() //display "Current animal is Tiger" gz.Display() //display "Current animal is Tiger" } ***/ `ZooDisplay` is a closure to capture the current variable state, here it's `gz`, and if we do so, the output of `pf2()` will always same as the output of `gz.Display()`. This should make it easier to understand and less error-prone. It's a little weird that the second `pf()` and `gz.Display()` get different results from the current design. I don't think we want to get different results later on just because of a simple assignment(`pf := gz.Display`). On Tuesday, August 10, 2021 at 3:05:03 PM UTC-7 bse...@computer.org wrote: > On Tue, Aug 10, 2021 at 3:50 PM E Z <lege...@gmail.com> wrote: > >> It works when I changed the code as your suggested. That's great, thanks. >> >> And I'm still a little confused here, you know when we use the struct >> method directly, it is only when the function is called that the type of >> receiver determines whether the passed struct is a pointer or a copied >> value. But when using a function pointer, why does it decide whether to >> bind a pointer or a copied value at the time of assignment, but not at the >> time of function called? >> >> It seems that these two behaviors are not consistent. Is there any >> benefit to doing so? I didn't find much information on this topic on >> Google. Is there any extended reading on this topic? >> > > It is consistent. The pointer to function assignment captures the current > receiver. > > func (z Zoo) Display() > func (z *Zoo) DisplayPtr() > > fptr:=gz.Display --> Captures gz, since Display gets gz by value, > captures gz by value > > fptr2:= gz.DisplayPtr -> Captures gz, since DisplayPtr gets gz by > reference, captures *gz > > That is: > > gz:=&Zoo{} > fptr2:=gz.DisplayPtr > gz=&Zoo{} > fptr2() -> This will call the DisplayPtr with the first value of gz, not > the second > > > >> >> On Tuesday, August 10, 2021 at 12:07:26 PM UTC-7 bse...@computer.org >> wrote: >> >>> On Tue, Aug 10, 2021 at 12:01 PM E Z <lege...@gmail.com> wrote: >>> >>>> I feel confused when I use the function pointer which point to the >>>> struct method. Here is the test code: >>>> >>>> /*** >>>> package main >>>> >>>> type Zoo struct { >>>> Animal string >>>> } >>>> >>>> func (z Zoo) Display(){ >>>> fmt.Printf("Current animal is:%s\n", z.Animal) >>>> } >>>> >>> >>> This method has a value receiver. When pf is assigned to gz.Display, it >>> is assigned with its receiver, which is a copy of gz. >>> >>> Change the method to func (z *Zoo) Display(), and it will work as you >>> expect. >>> >>> >>>> >>>> func main(){ >>>> gz := &Zoo{ >>>> Animal: "Monkey", >>>> } >>>> >>>> pf := gz.Display >>>> pf() //display "Current >>>> animal is Monkey" >>>> gz.Animal="Tiger" >>>> pf() //display "Current >>>> animal is Monkey" >>>> gz.Display() //display "Current animal is >>>> Tiger" >>>> } >>>> ***/ >>>> As the code is shown above, even though I changed the value of the >>>> member field of the Zoo instance gz, the function pointer call that >>>> followed still prints the unmodified value. >>>> >>>> Can someone help explain why? In my opinion, The function pointer pf >>>> should bind to the instance gz and its method(note that gz here is a >>>> pointer variable), if so anytime I change the value of the variable gz, >>>> pf should reflect the changes. >>>> >>>> Thanks, >>>> Ethan >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "golang-nuts" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to golang-nuts...@googlegroups.com. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/golang-nuts/60c6523c-dbce-4530-aa51-06f2c1223ca8n%40googlegroups.com >>>> >>>> <https://groups.google.com/d/msgid/golang-nuts/60c6523c-dbce-4530-aa51-06f2c1223ca8n%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com. >> > To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/651e712a-9f81-4b90-a56c-1fc4b4e8b174n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/651e712a-9f81-4b90-a56c-1fc4b4e8b174n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f7b29922-24dc-400d-a163-951ae1c0dd18n%40googlegroups.com.