No Nick, making Output() a member method won't work. See my OP and Jesse's answer. I.e., I have to change it from a member function to a pure function.
On Tue, Nov 22, 2016 at 6:25 PM, Nick Patavalis <nick.patava...@gmail.com> wrote: > Hi, > > There is no direct mapping of what you can do with virtual functions in > other OO languages, and Go. There are different compromises you have to > make; because of this, synthetic examples will probably not help much. > > That being said, in the case of your last example I would make Output() a > method of Animal. > The Speak() methods of the specific animals would then call Output(). > > /npat > > On Wednesday, November 23, 2016 at 12:03:54 AM UTC+2, Tong Sun wrote: >> >> >> >> On Tue, Nov 22, 2016 at 4:29 PM, Jesse McNelis wrote: >> >>> On Wed, Nov 23, 2016 at 8:16 AM, Tong Sun wrote: >>> > Hi, >>> > >>> > How to architect the OO's virtual function in Go? >>> > >>> > Please take a look at this (not working) Go program >>> > https://play.golang.org/p/qrBX6ScABp >>> > >>> > Please think of the "func Output()" as a very complicated function >>> that I >>> > only want to define once at the base level, not to duplicate into each >>> sub >>> > classes. >>> > >>> > How can I make it works so that the last output statement, instead of >>> being, >>> > >>> > fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area()) >>> > >>> > >>> > will be this instead: >>> > >>> > fmt.Printf("[%v] %s\n", k, v.Output()) >>> > >>> >>> You define a function: >>> >>> func Output(s Shape) string { >>> return s.Name() + "'s area size is " + s.Area() >>> } >>> >>> Go uses interfaces for polymorphism. >>> Other OOP languages can use inheritance for polymorphism too, but Go >>> doesn't have inheritance. >>> >> >> Thanks Jesse. That works. >> >> However I just realized that it is only part of my problem. >> >> I have a huge list of common variables that I defined in my "base class", >> changing it from a member function to a pure function causes almost every >> single variable now undefined. >> >> I can demonstrate the problem with this code >> https://play.golang.org/p/QjCtD9rGpa >> >> So, once again, thinking in OO, I'll define all of the common variables >> in base class, and common functionalities in virtual functions. How to make >> that idea work in Go? >> >> For the above specific code, how to easily make "func Output" works? >> >> Thanks >> >> -- > You received this message because you are subscribed to a topic in the > Google Groups "golang-nuts" group. > To unsubscribe from this topic, visit https://groups.google.com/d/ > topic/golang-nuts/f_62HEOIBV4/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- 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. For more options, visit https://groups.google.com/d/optout.