On Wed, Nov 23, 2016 at 8:16 AM, Tong Sun <suntong...@gmail.com> 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.

-- 
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.

Reply via email to