Hacker News new | past | comments | ask | show | jobs | submit login

One of the most common problems people have when they get to know Go is to not embrace its approach to programming, but try to enforce what they already know in Go programs.

This is a clear example of that. Implementing “polymorphism” with Go interfaces is abusing interfaces! Seeing embedding as inheritance misses the point of both embedding and inheritance. Comparing packages to namespaces isn’t that bad, but just delays you getting to know Go for real.

I’m not saying that these old concepts are bad or Go’s new approach is superior. (I believe in that; but that’s not the point here.) I’m saying if you’ve gotten used to these concepts so much that you can’t think or program without them, then there’s a problem. Don’t design a solution around polymorphism or inheritance and then try hard to force that into Go. Design your program with that your language is giving you.




Did you RTFA? The conclusion has:

> Composition, embedding and interfaces provide powerful tools for Object-Oriented Design in Go. Thinking in terms of inheritance really doesn't work. Trust me, I tried.

The OP is clearly trying to introduce ideas in Go by relating them to other ideas you might be more familiar with. This is, IMO, very much distinct from trying to shoehorn design techniques from other languages.

In fact, the OP is rather explicitly agreeing with your point and trying to remedy it.

> Implementing “polymorphism” with Go interfaces is abusing interfaces!

Go's interfaces use structural subtyping, which is a form of polymorphism.


I see pretty much the same problem with JavaScript. It's everywhere -- framework developers are busily implementing polymorphism, inheritence etc. all over the place. Yet I can't recall ever choosing to use "inheritance" instead of composition in my JavaScript projects.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: