Live data from Hacker News

Golang Object Oriented Design

nathany.com

1–10 of 75 posts

Re: Golang Object Oriented Design

#2
For an understanding how to do OO design in Go, learning about component programming is a good way.

http://www.amazon.de/Component-Software-Beyond-Object-Orient...

The first edition used Component Pascal from the Oberon family, which Go gets some influence from. Later editions used Java and C# instead.

Additional learning about how COM and XPCOM work is also a way to get some ideas.

Re: Golang Object Oriented Design

#4
Is there any advantage to using OO in a language that doesn'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't?

Re: Golang Object Oriented Design

#5
I keep wondering if Go couldn't have gone further in getting rid of the cruft that has accumulated around OO. Go doesn't have an implicit "this" pointer. It doesn't have members that are private to an individual object nor members private to a type. Encapsulation exists only at the package level.

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.

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's designers apparently intended to achieve.

Re: Golang Object Oriented Design

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

Re: Golang Object Oriented Design

#7
post #4

Is there any advantage to using OO in a language that doesn'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't?

OO does not have to use classes and inheritance. OO is about encapsulation of data and behaviors. Declaring a function on a type lets you specify the behaviors of the type. In particular, in languages like Go with interfaces, it allows you to have a contract of behaviors independent of implementations. You cannot do that with plain functions.

Re: Golang Object Oriented Design

#8
post #4

Is there any advantage to using OO in a language that doesn'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't?

> Is there any advantage to using OO in a language that doesn't have classes or inheritance?

Like Self or javascript?

Re: Golang Object Oriented Design

#9
post #4

Is there any advantage to using OO in a language that doesn'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't?

The lack of inheritance and creation methods are a bit annoying at times (particularly the latter), but there's still benefits of adopting an OOP approach in Go.

For example, say you're writing a CMS and want to make strings like the article title to be used in the URL (for readable URLs and SEO), displayed in HTML (eg inside heading tags) and also potentially used for string comparisons, you'd need to have a function to URL / HTML encode the string and you'd need to remember to do it each time you outputted the string. This can make it very easy to introduce vulnerabilities where you forget to HTML encode the string. With methods, you can force the developer to state which format to output the string as:

    type DisplayText struct {
        Value string
    }
    
    func (dt DisplayText) HTMLEscaped() string {
        return html.EscapeString(dt.Value)
    }
    
    func (dt DisplayText) URLEscaped() string {
        return url.QueryEscape(dt.Value)
    }
    
    var article_title DisplayText
So now when ever you call article_title, you have to specify the string encoding. Which is not only more secure (eliminates the risk of forgetting to encode your string), but also more readable:

    fmt.Println(article_title.HTMLEscaped)
vs

    fmt.Println(html.EscapeString(article_title))

Re: Golang Object Oriented Design

#10

I keep wondering if Go couldn't have gone further in getting rid of the cruft that has accumulated around OO. Go doesn't have an implicit "this" pointer. It doesn't have members that are private to an individual object nor members private to a type. Encapsulation exists only at the package level. Yet Go keeps that old OO concept of method receivers. Why? I never found it very plausible to have one special case parame…

Without method receivers you wouldn't have methods, only funcs. Then you'd end up calling your "methods" names like `Address_Compare(addr1, addr2 Address)`, `Address_ToString(addr Address)` and so forth.

How unpleasant that'd be! With method receivers, you can have `addr1.CompareTo(addr2)` and `addr.ToString()` etc..

Post reply on HN