

Go's unique approach to OO - luriel
http://www.drdobbs.com/article/print?articleId=240005949&siteSectionName=open-source

======
acqq
Does anybody know how Go manages to call the given function of the matching
interface when it gets only a pointer to the struct? Like here, where label
and button are otherwise unrelated structs but both have the same Paint
function:

    
    
       for _, painter := range []Painter{label, listBox, button1, button2} {
            painter.Paint()
       }
    

In C++, where inheritance is used, the virtual function pointer tables are
maintained and the inherited class will have the function at the same index
which makes calls of virtual functions slower than normal calls but the cost
is still just an indexing of the table. However when there is no inheritance
specification, the indexes can't be maintained, I presume. The easy solution
is that calls are resolved using some string fingerprint lookup but that would
make such calls really slow (compared to the C++).

Then how do they do it?

~~~
ralph
This post by rsc should answer your questions and more.
<http://research.swtch.com/interfaces>

~~~
acqq
Thanks, it seems it fully answers my question!

------
stcredzero
Preferring composition to inheritance is hardly unique or new. However, having
that designed into the language is far less common than it should be.

~~~
tdavis
Agreed. As is so often the case, "unique" is utterly misused here. That being
said, having used Go, I really do enjoy the composition-at-core design.

~~~
sirclueless
"Unique" is a fair description of Go's approach to OO: The ideas it is based
around may not be totally original, but the implementation is different from
any language in common use.

------
huanzhou_2006
I think there are some obvious mistakes in it: 1) The NewXXX() function should
return a pointer by Go idioms. But they return the value in this article. 2)
For methods on IntSet type, it says the receiver should be pointer instead of
value. "If we used values, these would be copied and any changes applied
lost." Actually, since the data field is a reference, using value as receiver
can change the content of the map.

Maybe not a good tutorial for people new to Go.

~~~
objclxt
In my opinion, the _best_ tutorial for those new to Go is the official Go
tour:

<http://tour.golang.org/>

------
mhd
Isn't Go's approach quite similar to what Wirth used in Oberon? (Which the
Plan 9 guys really like to copy in general)

------
djhworld
Upvote for linking to the printer friendly version of the article

