Notes from the course Learn How to Code: Google’s Go Programming Language
Go and Types
Like all languages values are of certain types. Types can be int, string, boolean, etc. Within Go, you can also define your own types.
[pastacode lang=”c” manual=”type%20hacker%20bool%0A%0Afunc%20main()%7B%0A%09var%20eliot%20hacker%20%3D%20true%0A%09fmt.Println(eliot)%0A%09fmt.Printf(%22%25T%20%22%2C%20eliot)%0A%7D” message=”” highlight=”” provider=”manual”/]
In the above example, eliot is set to a new type called “hacker.” As we know in Mr. Robot, Eliot is a hacker, so this type has the underlying type of boolean.
In the main function, eliot is set to true. Verifying this and its type is done with the Print statements, which output:
OOP and Go
In a post from 2013, Bill Kennedy writes:
I consider Go to be a light object oriented programming language. Yes it does have encapsulation and type member functions but it lacks inheritance and therefore traditional polymorphism. For me, inheritance is useless unless you want to implement polymorphism. With the way interfaces are implemented in Go, there is no need for inheritance. Go took the best parts of OOP, left out the rest and gave us a better way to write polymorphic code. –From Goinggo.net
If you search long enough, you’ll soon discover that this topic is covered (and debated) quite a bit. Yes, Go is Object Oriented, but it lacks the traditional elements we associate with Object Oriented Languages (like classes, inheritance, etc.)
In Todd McLeod’s course on Go [link] he states that Go is OOP with these elements you would find in other OOP languages:
- Reusability through inheritance or embedded types
- Polymorphism through interfaces
- Overriding through promotion
The above seems a tad contrary to some of the other links and discussion that I’ve come across. However, Todd makes a good argument for these points.
Regarding Encapsulation, Todd states that by creating our own types and that type has fields that hold state. Those types can be associated to behaviors (like methods.)
For Inheritance, Todd adequately shows in the course that a type can be injected anonymously into code. When done, all the fields that were part of the struct are carried (promoted) with the injection.
[pastacode lang=”c” manual=”type%20Car%20struct%20%7B%0A%09make%20string%0A%09model%20string%0A%09year%20int%0A%7D%0A%0Atype%20Demolition%20struct%20%7B%0A%09Car%0A%7D” message=”” highlight=”” provider=”manual”/]
As for Polymorphism, Todd McLeod explains that interfaces fit the bill.
Finally, in regards to Overriding methods/functions, Todd McLeod shows that through “promotion” a function can be overridden. This is a complex subject for me. However, in watching and rewatching the lecture several times I have an idea of what he’s getting at. Below is a code example I wrote, that follows his own:
[pastacode lang=”c” manual=”%0Atype%20Car%20struct%20%7B%0A%09Make%20string%0A%09Model%20string%0A%09Year%20int%0A%7D%0A%0Atype%20Demolition%20struct%20%7B%0A%09Car%0A%09Make%20string%0A%09Convertable%20bool%0A%7D%0A%0Afunc%20main()%7B%0A%09mustang%20%3A%3D%20Demolition%7B%0A%09%09Car%3A%20Car%7B%0A%09%09%09Make%3A%20%22Ford%22%2C%0A%09%09%09Model%3A%20%22Mustang%22%2C%0A%09%09%09Year%3A%201973%2C%0A%09%09%7D%2C%0A%09%09Make%3A%20%22FORD%20MOTORS%22%2C%0A%09%09Convertable%3A%20true%2C%0A%09%7D%0A%0A%09fmt.Println(mustang.Make%2C%20mustang.Model%2C%20%20mustang.Year%2C%20mustang.Car.Make)%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
In the above code the output will be:
FORD MOTORS Mustang 1973 Ford
mustang.Make prints out “FORD MOTORS” and mustang.Car.Make will return the value “Ford.” What occurred here is that the Car struct was promoted into the Demolition struct and and when we set a value to Demotion, we created a set of values for the mustang car. However, another definition of “Make:” was created which shows overriding the initial value.
Structs in Go
The reason why I started with the concept of Types is that the instructor (Todd McLeod) brought up the idea first before going into Structs. Here’s why – When making Structs in Go, you define a type of struct.
A struct is a data set that resembles (at a glance) something akin to a class in other languages. Example:
[pastacode lang=”c” manual=”type%20car%20struct%20%7B%0A%09make%20string%0A%09model%20string%0A%09year%20int%0A%7D%0A%0Afunc%20main()%7B%0A%09mustang%20%3A%3D%20car%7B%22Ford%22%2C%20%22Mustang%22%2C%201973%7D%0A%09fmt.Println(mustang.make%2C%20mustang.model%2C%20%20mustang.year)%0A%7D” message=”” highlight=”” provider=”manual”/]
The value mustang is of type car. It is reminiscent of a class being instantiated as objects. I’m not sure if that’s what’s actually happening… and my understanding of the deep waters of OOP is quite limited.