200 points · by markkit · 10y ago · 46 comments
Simply mapping familiar vocabulary from an traditional OO language into Go implies that the differences are superficial and the underlying concepts are largely the same. The truth is that a lot of the traditional vocabulary was very intentionally left out of Go.
For example, mapping embedded stucts to "multiple inheritance" masks the fact that Go is trying to guide you into understanding the power of composition over inheritance. It's not that it's impossible to understand that, but by carrying over the terminology, you burden yourself with some preconceptions.
Like I said, I know this is a work in progress, but I encourage you to perhaps map the terms but then write a paragraph or two about how they're _different_, not the same.
This was a really nice resource for me when I first came to Go: http://spf13.com/post/is-go-object-oriented/
I find that what has been "intentionally left out of Go" is not necessarily a good choice.
I also don't get the obsession with "idiomatic" programming.
Idiomatic Java/J2EE circa 1998-2008 was total crap (the programming style, not the language). Even its then adherents admit it. Same holds for many other languages/times.
Programmers should be consistent with a style, and teams should work with an agreed upon style. But some languages designers to impose their ideas upon all users of a language is wrong.
I think part of the aim of Go was to make this style impossible, by e.g. removing inheritance, which plays a key role in most crappy Java code.
Want to write a linked list that works on all types? Hope you like reflection or annoying code generation or copy and paste.
First: I took a brief look through this, and I didn't see anything that jumped out as wrong. It's a pretty comprehensive document, especially for someone who says they're just starting out, so kudos to James for writing it!
That said, from my experience in teaching Go classes and workshops for beginners, comparing embedding to inheritance is usually a bad idea. It's not that there aren't similarities, but in my experience it prompts new Go programmers to write unidiomatic Go code when they think of embedding in that way. They end up embedded structs the way they'd use inheritance in object-oriented languages, and since Go isn't really intended as an object-oriented language, it makes for unidiomatic code.
The main use cases of inheritance in OOP are accomplished by interfaces in Go, not struct embedding. But at the same time, the main benefits of embedding come from embedding interfaces - embedding structs is almost an afterthought in the language. In fact, I tend to advise people against embedding structs altogether. There are a few cases where it's worthwhile to embed structs, but the use case for embedding interfaces is much more powerful.
My advice for Go beginners: use interfaces more than you think you need to, and forget that struct embedding is even possible. This rule of thumb helps to write more idiomatic Go code when you're just getting started, before you have a feel for what that really means.
If anything, C++ is less object-oriented than Go, and more oriented towards abstract data types (which C++ programmers call “concepts”).
Go makes classic inheritance impossible, it is a bit more than discouraging it.
(0) Embed base structs.
(1) Implement non-overridden methods as redirected calls to base struct methods.
(2) Implement overridden method calls however you wish.
What Go doesn't make possible is tying inheritance to subtyping, like Java and C++ do. And this is a very good thing.
I haven't put it into practice, but it seems like this should give callers enough flexibility to pass in whatever they like.
You can have `datastore.New() Datastore`, which returns one of the implementations, or you can have `datastore.NewMySQLStore, datastore.NewInMemStore()`. Now you need a different method for each implementation.
Also be careful with returning concrete type, you can run into this issue: https://play.golang.org/p/WHAW1X6elS
In general, using struct embedding to simulate sharing code and data is pretty limited. I rarely use this feature in my own code. However, having structs which automatically implement interfaces is awesome because you can "say what you need." You can also compose the interfaces together by embedding them. For example of that, checkout the Map type in my data-structures repository: https://godoc.org/github.com/timtadh/data-structures/types#M...
This happens within the standard library in ways that hurt performance. The 'image' package allows people to register their own decoders but limits the API to an io.Reader. It can be an optimization if you could cast the inputted reader to another more efficient type (like a ReaderAt), but you can't because the image package wraps the Reader in its own "peeking" type. There's no way to tell the type was embedded, or what other actual methods might have worked.
The parameter is actually quite explicit (unlike some other languages).
It would probably help your write up to talk about the difference between (using your example):
func (r Rectangle) Area() float64 {
and func Area(r Rectangle) float64 {
As well as (this is a common rookie mistake): func (r Rectangle) SetWidth(w float64) {
r.Width = w // does nothing
and func (r *Rectangle) SetWidth(w float64) {
r.Width = wOther than that, nice article.
Privacy is only enforced between packages, so anything in the same package as the struct can see the private fields.
So, on to the spec: https://golang.org/ref/spec#unicode_letter
> In The Unicode Standard 8.0, Section 4.5 "General Category" defines a set of character categories. Go treats all characters in any of the Letter categories Lu, Ll, Lt, Lm, or Lo as Unicode letters, and those in the Number category Nd as Unicode digits.
Which reads:
Lu = Letter, uppercase
Ll = Letter, lowercase
Lt = Letter, titlecase
Lm = Letter, modifier
Lo = Letter, other
So I suppose (but am not sure) the case you describe falls into Lo. type foo struct {
a int // "private"
B int // "public"
}
You cannot specify default values and Go refers to them as "exported" not private, but technically they are "private" in the sense that they can only be accessed via methods.If that is a concern, you can create a new package for your type.
1. the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
2. the identifier is declared in the package block or it is a field name or method name.
All other identifiers are not exported.
https://golang.org/ref/spec#Exported_identifiers
Both of the characters you asked about are lowercase:
http://www.fileformat.info/info/unicode/char/00df/index.htm
http://www.fileformat.info/info/unicode/char/0138/index.htm
Here's the full list of Lu characters:
http://www.fileformat.info/info/unicode/category/Lu/list.htm
An identifier may be exported to permit access to it from another package.
An identifier is exported if both:
the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
the identifier is declared in the package block or it is a field name or method name.
All other identifiers are not exported.