Golang for OO Programmers

19
GoLang for OO Programmers Khalid Nowaf COSC 419 Special Topic : Golang and Docker Computer Science Okanagan College, 2016

Transcript of Golang for OO Programmers

GoLang for OO Programmers

Khalid Nowaf COSC 419Special Topic : Golang and DockerComputer ScienceOkanagan College, 2016

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

Agenda

Struct Types ”As Class” Constructor and Custom Constructor “Factory” Private and Public Struct Members Embedded types (Composition) Shadowing Fields and Methods Interfaces

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

Classes

Fields Methods

From Khalid-Nowaf/animal/animal.go

Object

In Go there is no such thing called Objects , but there is Types !

How to use ”this” keyword in Go ?

Constructor

Go has Built-in Constructor, but limited.

From Khalid-Nowaf/animal/animal.go

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

Private vs Public Members

There is only one rule Capital is Public ”exported”Small is Private “unexported”

From Khalid-Nowaf/animal/animal.go

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”

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

Embedding and Shadowing

embedded.go “main”

output

embedded.go “Package”

Multiple Embedding

multiEmbedded.go “Package”

Multiple Embedding In Action

multiEmbedded.go (main)

More In Shadow Methods

Emedded.go “package”

Emedded.go “main”

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

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

Interfaces = Polymorphism In Action

Since the code will not fit the slide ! , lets open {GOPATH}/src/github.com/Khalid-Nowaf/animal/animal.go

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