TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Golang Object Oriented Design

151 pointsby JanLaussmannover 11 years ago

9 comments

fauigerzigerkover 11 years ago
I keep wondering if Go couldn&#x27;t have gone further in getting rid of the cruft that has accumulated around OO. Go doesn&#x27;t have an implicit &quot;this&quot; pointer. It doesn&#x27;t have members that are private to an individual object nor members private to a type. Encapsulation exists only at the package level.<p>Yet Go keeps that old OO concept of method receivers. Why? I never found it very plausible to have one special case parameter with its very own syntax, but with type level encapsulation it did at least have some justification.<p>Without this kind of encapsulation, the only remaining purpose of the receiver is method dispatch. But why dispatch based on the type of one parameter and not the others? Multimethods would have been the logical conclusion of the OO cleanup that Go&#x27;s designers apparently intended to achieve.
评论 #6392937 未加载
评论 #6395228 未加载
评论 #6392939 未加载
评论 #6392972 未加载
评论 #6398001 未加载
评论 #6393000 未加载
评论 #6393003 未加载
drunken_thorover 11 years ago
Here is the article that got me onto the way of go: <a href="http://areyoufuckingcoding.me/2012/07/25/object-desoriented-language/" rel="nofollow">http:&#x2F;&#x2F;areyoufuckingcoding.me&#x2F;2012&#x2F;07&#x2F;25&#x2F;object-desoriented-...</a> The memes are a bit much but the content is solid.
pjmlpover 11 years ago
For an understanding how to do OO design in Go, learning about component programming is a good way.<p><a href="http://www.amazon.de/Component-Software-Beyond-Object-Oriented-Programming/dp/0201745720" rel="nofollow">http:&#x2F;&#x2F;www.amazon.de&#x2F;Component-Software-Beyond-Object-Orient...</a><p>The first edition used Component Pascal from the Oberon family, which Go gets some influence from. Later editions used Java and C# instead.<p>Additional learning about how COM and XPCOM work is also a way to get some ideas.
评论 #6394148 未加载
munificentover 11 years ago
&gt; To apply the uniform access principle to the Part type, we could change the struct definition and provide setter&#x2F;getter methods<p>If you have to <i>apply</i> it, it isn&#x27;t <i>uniform</i> access. The point of the principle is that the call-site shouldn&#x27;t distinguish between getters and fields so that the implementer can change that decision freely without breaking callers.<p>Nice article, though!
评论 #6397341 未加载
hergeover 11 years ago
I have a question about go. Let&#x27;s say I have a struct Foo and a function&#x2F;method for Less. What is the quickest way to sort an array of Foo&#x27;s?<p>Do I have to implement my own []Foo type with all three methods as described in the sort package?
评论 #6393278 未加载
Toucheover 11 years ago
Is there any advantage to using OO in a language that doesn&#x27;t have classes or inheritance? Does declaring a method on a type give you something that declaring a function that takes the type as an argument doesn&#x27;t?
评论 #6392898 未加载
评论 #6393008 未加载
评论 #6392962 未加载
评论 #6392915 未加载
评论 #6392903 未加载
评论 #6394835 未加载
评论 #6393356 未加载
评论 #6394431 未加载
mostafahover 11 years ago
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.<p>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.<p>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.
评论 #6393363 未加载
评论 #6392979 未加载
crazygringoover 11 years ago
Off-topic, but it would be nice if the blog didn&#x27;t use <i>24-pixel body type</i>, so that I could see more than a handful of lines at a time on my laptop.<p>I&#x27;ve never seen a book with 24-point body type, except possibly for the visually impaired or 3-year-olds. Heck, most <i>headings</i> aren&#x27;t even 24 point. What is it with this blog trend of gargantuan type, which seems increasingly common? It&#x27;s totally out of proportion with the rest of anyone&#x27;s OS and computer interface, and all the common sites. I&#x27;m really getting tired of having to zoom <i>out</i> to an insane <i>50%</i> level just to make things legible again.
评论 #6397339 未加载
noelherrickover 11 years ago
Good article - this covers the basic OO patterns in Go. I really like the simplicity of the design - objects are just structs and you can define a method for that object. Coming from C#, it&#x27;s like every method is an extension method.<p>The only critique I have is that there&#x27;s no way to verify that object X implements methods A, B, and C. When I&#x27;m using C# or Java, I often use an interface as a test to make sure I&#x27;ve done my work since the code won&#x27;t compile if a class that extends an interface fails to implement all the methods.
评论 #6394489 未加载
评论 #6397390 未加载
评论 #6394488 未加载