Find this article and more at Golang Code Examples
This golang code sample demonstrates the following go language features:
-
string and float64 data types
-
constants
-
variables with initializers
-
iterating ranges
-
slices
-
encapsulation / visibility
-
variadic functions
-
new function
-
pointers
-
structs / struct literals
-
methods
-
multiple implicit interfaces
-
signature based polymorphism
Code Example
Output
Notes
Nearly every line of this code example is documented; Scroll to the right to see all of the comments.
This code example shows how to implement a solution using object oriented techniques in Golang.
Golang does not use a
class keyword but is none-the-less object oriented.
Methods and interfaces are the language constructs that define objects and their behavior.
There is no explicit implementation inheritance; However, type embedding can be used for the same purpose.
Golang includes very little ceremony in order to provide language features.
-
no semi-colons at end of each statement
-
little declaration required - golang performs implicit type conversion and eliminates need for var keyword with ":=" operator
-
visibility and encapsulation - Some languages require you to use public, private, friend, etc. keywords. Golang simply looks for capitalization of function/struct names.
-
A type implements an interface by implementing the methods - Some languages require explicit declaration of intent; You don't have to find every interface implementation and label it with the new interface name.
By adding a receiver to a function definition, Golang allows you to associate that method with a structure, whereby you can use the "." operator to call the associated method.
Golang does not have a self or this keyword to reference to the current instance. In the method example of
func (c *Circle) area() float64 the receiver struct is named "c". Use that variable name, rather than this to refer to the current instance.
Golang uses signature based polymorphism; If a struct, in the example above, implements an area method that returns a float64, then it is a Shape.
This is what some call "Duck Typing" meaning, "If it walks like a duck and sounds like a duck then it must be a duck."
In our case, if the struct implements the area method then it is a Shape type struct.
You can define a method on any type you define in your package, not just a struct.
You cannot define a method on a type from another package, or on a basic type.
Methods can be associated with a named type
func drawShape(d Drawer) or a pointer to a named type
func (c *Circle) area() float64.
Benefits of using a pointer receiver:
-
Avoid copying value of parameter on each method call
-
Allow method to modify the value that its receiver points to
Golang is a statically typed language, which has some significant benefits over dynamic languages like:
-
Compiler catches errors that could be otherwise hard to find runtime errors
-
Better documentation via type signatures that include type of arguments
-
Better error reporting, sooner. Compiler will report line number and indicate exactly what caused the bug.
-
Code runs faster
The first one is especially important when your code base grows.
A few important Golang topics not discussed in this post include closures, concurrency, error handling and testing.
A more notable exclusion is lack of mention of
Embedding. which is an OO technique used by Go to inherit/embed the implementation from another class.
Go does not use the
class keyword, but I refer to a Go class as a struct and it's associated methods, which combined encapsulates the properties and behavior of a "class" of objects.
References
http://www.golang-book.com/
http://golang.org/doc/code.html
http://tour.golang.org/
This work is licensed under the
Creative Commons Attribution 3.0 Unported License.