Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Golang for OO Programmers

906 views

Published on

a quick overview of Golang for Object oriented programmers

Published in: Technology
  • What is non-blocking channel operation?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Golang for OO Programmers

  1. 1. GoLang for OO Programmers Khalid Nowaf COSC 419 Special Topic : Golang and Docker Computer Science Okanagan College, 2016
  2. 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. 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. 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
  5. 5. Classes Fields Methods From Khalid-Nowaf/animal/animal.go
  6. 6. Object In Go there is no such thing called Objects , but there is Types ! How to use ”this” keyword in Go ?
  7. 7. Constructor Go has Built-in Constructor, but limited. From Khalid-Nowaf/animal/animal.go
  8. 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. 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. 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. 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
  12. 12. Embedding and Shadowing embedded.go “main” output embedded.go “Package”
  13. 13. Multiple Embedding multiEmbedded.go “Package”
  14. 14. Multiple Embedding In Action multiEmbedded.go (main)
  15. 15. More In Shadow Methods Emedded.go “package” Emedded.go “main”
  16. 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. 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. 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. 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

×