On Tue, Aug 10, 2021 at 3:50 PM E Z <legend...@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+unsubscr...@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/CAMV2RqrifLCDEHyRqNH6vzYVV0hmH9VGfpf55pJQSJy9QdGsPg%40mail.gmail.com.