Nice introductory article on why Golang is not OOP
Table of Contents
It is a copy of this article — https://rakyll.org/typesystem/
It is real struggle to work with a new language, especially if the type doesn’t resemble what you have previously seen. I have been there with Go and lost my interest in the language when it first came out due to the reason I was pretending it is something I already knew.
Go is considered as an object-oriented language even though it lacks type hierarchy. It has an unconventional type system. It is expected to do the things differently in this language given the traditional paradigms are not always going to help the Go users. This article contains a few gotchas.
Program flow first, types later
In Go, program flow and behavior are not tightly coupled to the abstractions. You don’t start programming by thinking about the types but rather the flow/behavior. As you need to represent your data in more sophisticated ways, you start introducing your types.
More recently Rob Pike shared his thoughts on the separation of data and behavior:
… the more important idea is the separation of concept: data and behavior are two distinct concepts in Go, not conflated into a single notion of “class”. – Rob Pike
Go has a strong emphasis on the data model. Structs (which are aggregate types) provide a light-weight way to represent data. The lack of type hierarchy helps structs to keep being thin, structs never represent the layers and layers of inherited behavior but only the data fields. This makes them closer to the data structures they represent rather than the behavior they are additionally providing.
Embedding is not inheritance
Code reuse is not provided by type hierarchy but via composition. Language ecosystems with classical inheritance is often suffering from excessive level of indirection and premature abstractions based on inheritance which later makes the code complicated and unmaintainable.
Instead of providing type hierarchy, Go allows composition and dispatching of the methods via interfaces. The language allows embedding and most people assume the language has some limited support for sub-classing types – this is not true.
Embedding is really not very different than having a regular field but allows you to embed the methods on the embedded type directly into the new type.
Consider the following struct:
1 2 3 4 5 |
type File struct { sync.Mutex rw io.ReadWriter } |
Then, File objects will directly have access to sync.Mutex methods:
1 2 3 |
f := File{} f.Lock() |
It is no different than providing Lock and Unlock methods from File and make them operate on a sync.Mutex field. This is not sub-classing.
Polymorphism
Due to lack of sub-classing, polymorphism in Go is achieved only with interfaces. Methods are dispatched during runtime depending on the concrete type.
1 2 3 4 5 6 7 8 9 |
var r io.Reader r = bytes.NewBufferString("hello") buf := make([]byte, 2048) if _, err := r.Read(buf); err != nil { log.Fatal(err) } |
Above, r.Read will be dispatched to (*Buffer).Read.
Please note that embedding is not sub-classing, embedding types can not be assigned to what they are embedding. The following code is not going to compile:
1 2 3 4 5 6 7 8 9 10 11 12 |
type Animal struct {} type Dog struct { Animal } func main() { var a Animal a = Dog{} } |
No explicit interface implementations
Go doesn’t have an implements
that explicitly allowing you to tell you are implementing an interface. It assumes you are implementing an interface if the method signature matches the one in the interface definition.
How does this scale? Is it possible to accidentally implement interfaces you didn’t mean to implement? Although mechanically possibly, it has never been an issue for our user base to pass an implementation of one interface mistakenly for another one. Interfaces often are widely different, or it is sign there might not be a need of a second interface if two interfaces are quite similar.
We have a culture of not introducing new interfaces but prefer to use the ones provided by the standard library or use the established ones from the community. This culture also reduces the number of similar looking interfaces.
No header files
No header files or no culture of “let’s introduce interfaces first”. If you don’t want to provide multiple implementations of the same high-level behavior, you don’t introduce interfaces.
Naming patterns based on other languages’ dependency inversion conventions are anti-patterns in Go. Naming styles such the following don’t fit into the Go ecosystem.
1 2 3 4 5 |
type Banana interface { //... } type BananaImpl struct {} |
One more thing… Go prefer small interfaces. You can always embed interfaces later but you cannot decompose large ones.
No constructors
Go doesn’t have constructors hence doesn’t allow you to override the default constructor. Default construction always result in zero-valued fields.
Go has a philosophy to use zero-value to represent the default. Utilize zero-value as much as possible to provide the default behavior.
Some structs may require more work such as validation,opening a connection, etc before becoming useful to the user. In such cases, we prefer initialization functions.
1 2 |
func NewRequest(method, url string, body io.Reader) (*Request, error) |
NewRequest validates method and url, sets up the right internals to read from the given body and returns a request.
Nil receivers
Nil is a value, nil value of a type can implement behavior. Developers don’t have to provide concrete types for noop implementations.
If you are introducing an interface, only to provide a noop implementation of your concrete type, don’t do it.
Below, event logging will be a noop for the nil values.
1 2 3 4 5 6 7 8 9 |
type Event struct {} func (e *Event) Log(msg string) { if e == nil { return } // Log the msg on the event... } |
Then user can use the nil value for the noop behavior:
1 2 3 |
var e *Event e.Log("this is a message") |
No generics
Go doesn’t have generics.
There are ongoing conversations happening on what kind of generics would be a good fit for Go. Given the unique type system, it is not easy to just copy an existing approach and assume it will be useful for the majority and will be orthogonal to the existing language features. Go Experience Reports are waiting for user input to see what kind of use cases could have helped the Go users if Go had generics.
Similar Posts
LEAVE A COMMENT
Для отправки комментария вам необходимо авторизоваться.