Golang Object Oriented Design
nathany.com
Golang Object Oriented Design
1–10 of 75 posts
Re: Golang Object Oriented Design
#2http://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
#3Re: Golang Object Oriented Design
#4Re: Golang Object Oriented Design
#5Yet 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
#6This 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
#7Is 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
#8Is 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?
Like Self or javascript?
Re: Golang Object Oriented Design
#9Is 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?
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
#10I 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…
How unpleasant that'd be! With method receivers, you can have `addr1.CompareTo(addr2)` and `addr.ToString()` etc..