New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Golang for OO Programmers
1. GoLang for OO Programmers
Khalid Nowaf COSC 419
Special Topic : Golang and Docker
Computer Science
Okanagan College, 2016
2. Before We Start
Import The presentation Packages by using this Commands
go get github.com/Khalid-Nowaf/goExample
go get github.com/Khalid-Nowaf/animal
go get github.com/Khalid-Nowaf/embedded
You should find all the code in this path
{GOPATH}/src/github.com/Khalid-Nowaf
The goExample package include the main files for our examples, where animal and embedded
packages just the implementation
You should run any file in goExample by using this command
go run {filename}
E.g. go run class.go
3. Agenda
Struct Types ”As Class”
Constructor and Custom Constructor “Factory”
Private and Public Struct Members
Embedded types (Composition)
Shadowing Fields and Methods
Interfaces
4. Is GoLang OO ?
Classes
Encapsulation
Inheritance
Interfaces
Composition
delegation
Polymorphism
“ Objects are like people. They’re
living, breathing things that have
knowledge inside them about how to
do things and have memory inside
them so they can remember things.
And rather than interacting with
them at a very low level, you interact
with them at a very high level of
abstraction, like we’re doing right
here. “Steve Jobs
8. Custom Constructor
To simulate the Constructor we create a method that return a new
instance . AKA Factory in Design pattern
.
.
.
From Khalid-Nowaf/animal/animal.go
9. Private vs Public Members
There is only one rule
Capital is Public ”exported”
Small is Private “unexported”
From Khalid-Nowaf/animal/animal.go
10. Classes In Action
To follow me make sure you Import my package by using this commands
go get github.com/khalid-nowaf/animal
go get github.com/khalid-nowaf/goExample
You will find the code in this path
{GOPATH}/src/Khalid-Nowaf/animal Code implementation “package”
{GOPATH}/src/Khalid-Nowaf/goExample Code Execution “main”
11. Inheritance and Composition
Embedding akin to multiple inheritance
embedded field named as “anonymous field”
Let's call "base" the struct embedded
And "derived" the struct doing the embedding.
Base fields and methods are available as if they were declared in the
derived struct.
But base fields and methods can be "shadowed"
Shadowing: defining another field or method with the same name
(and signature) of a base field or method
15. More In Shadow Methods
Emedded.go “package”
Emedded.go “main”
16. Multiple inheritance and The Diamond Problem
Golang solves the diamond problem by not allowing
diamonds.
all embedded class-field-names should not collide
You must rename fields if there is a name collision
This rule avoids the diamond problem, by not allowing it
17. Interfaces
The interface in Golang is designed to complement structs
Struct: has fields and NON-VIRTUAL methods
While, Interface has with NO fields, ALL VIRTUAL methods
you can use an interface to:
Declare a var or parameter of type interface.
implement an interface, by declaring all the interface virtual
methods in a concrete class (a struct)
Inherit (embed) a golang-interface into another golang-
interface
18. Interfaces = Polymorphism In Action
Since the code will not fit the slide ! , lets open
{GOPATH}/src/github.com/Khalid-Nowaf/animal/animal.go
19. To Sum-up …
A golang struct-method
It is defined outside of the class(struct) body
extra section before the method name to define the "receiver" (this)
Structs vs Interfaces
Structs: **classes, with fields, ALL NON-VIRTUAL methods
Interfaces: **classes, with NO fields, ALL VIRTUAL methods
Interfaces used to impalement Polymorphism
Shadowing: means defining another field or method with the same name (and
signature) of a base field or method
GoLang can act as OOP if you wish , but its not by design
Notas do Editor
Abstraction: The process of picking out (abstracting) common features of objects and procedures.
Class: A category of objects. The class defines all the common properties of the different objects that belong to it.
Encapsulation: The process of combining elements to create a new entity. A procedure is a type of encapsulation because it combines a series of computer instructions.
Information hiding: The process of hiding details of an object or function. Information hiding is a powerful programming technique because it reduces complexity.
Inheritance: a feature that represents the "is a" relationship between different classes.
Interface: the languages and codes that the applications use to communicate with each other and with the hardware.
Messaging: Message passing is a form of communication used in parallel programming and object-oriented programming.
Object: a self-contained entity that consists of both data and procedures to manipulate the data.
Polymorphism: A programming language's ability to process objects differently depending on their data type or class.
Procedure: a section of a program that performs a specific task.
Embedding: a struct into another you have a mechanism similar to multiple inheritance with non-virtual members.
Shadowing means defining another field or method with the same name (and signature) of a base field or method.
Once shadowed, the only way to access the base member is to use the hidden field named as the base-struct-name.
the only way to access the base member is to use the hidden field named as the base-struct-name.
All base members can be accessed via the hidden field named as the base-struct-name.
all inherited methods are called on the hidden-field-struct. It means that a base method cannot see or know about derived methods or fields. Everything is non-virtual.
When working with structs and embedding, everything is STATICALLY LINKED. All references are resolved at compile time.
Rmove e ..
Since all golang-struct methods are non-virtual, you cannot override methods (you need interfaces for that)
If you have a method show() for example in class/struct NamedObj and also define a method show() in class/struct Rectangle, Rectangle/show() will SHADOW the parent's class NamedObj/Show()
As with base class fields, you can use the inherited class-name-as-field to access the base implementation via dot-notation
var a derived = derived{base{"base-a", 10}, 20, 2.5}
a.display() // calls Derived/display(a)
// => "derived, a is: 2.5"
a.base.display() // calls Base/display(a.base), the base implementation
// => "base, a is: base-a"
a.xyz() // "xyz" was not shadowed, calls Base/xyz(a.base)
// => "xyz, a is: base-a"
By restricting structs to non-virtual methods, and restricting interfaces to all-virtual methods and no fields. Both elements can be perfectly combined by embedding to create fast polymorphism and multiple inheritance without the problems associated to multiple inheritance in classical OOP