Go notes (OOP topics) πŸ‡¬πŸ‡§

Go notes (OOP topics) πŸ‡¬πŸ‡§

- 16 mins

In this post, I would like to share the notes that I took when I was completing following Go series education on Coursera. I can recommend it for anyone who would like to get rapid introduction to Go programming.

Programming with Google Go

Go course certificate

I should admit that although it looks fancy, nothing can be compared to actual development and contribution to open source projects. The course itself is quite interesting and contains very handy exercises regarding to Go development mentality. I would recommend it for anyone who has some interest in Go development and do not know where to start.

There will be following headers and subheaders which contains some notes on Go Programming language as bulletpoints.

OOP in Go

In Go, there is no such a concept of object oriented programming as in Java, however, it is possible to imply similar approach (object oriented approach) in Go using interfaces and structs.

Classes

There is no β€œClass” keyword in Go.

Classes are supported with structs in Go.


type Point struct {
    X float64
    Y float64
}


func (p Point) DistToOrig() {
  t:= math.Pow(p.x,2) + math.Pow(p.y,2) 
  return math.Sqrt(t)
}

func main() {
	p1 := Point(3,4)
	fmt.Println(p1.DistToOrig())
}

Objects

Encapsulation

Encapsulation is supported as following ;

Create a package called data which has exported function which is PrintX, since it starts with capital letter, in Go, when something starts with capital letter, it means that it is exported

package data 
var x int=1
func PrintX() {
    fmt.Println(x)
}

Main package which is starting point of any application in Go.


package main 
func main() {
    data.PrintX()
}

Controlling access to Structs


package data 

type Point struct{
	x float64
	y float64
}

func (p *Point) InitMe(xn,xy float64) {
	p.x =xn
	p.y =xy
}

func (p *Point) Scale(v float64) {
		p.x=p.x*v
		p.y=p.y*v
}

func (p *Point) PrintMe() { 
	fmt.Println(p.x,p.y)
}

Note that all methods are public ! Since their initial character is capital letter.

package main

func main() {
    var p data.Point 
    p.InitMe(3,4)
    p.Scale(2)
    p.PrintMe()
}

Limitation of Method

package main

func main(){
	p1:=Point(3,4)
	p1.OffsetX(5)
}

Large Receivers


type Image [100] [100]int 
func main() { 
	i1 := GrabImage()
	i1.BlurImage()
}

10.000 ints copied to BlurImage() (Pitfalls)

Pointer Receivers

func (p *Point) OffsetX (v float64) {
	p.x=p.x+v
}

Point Receivers, Referenceing, Dereferencing

func (p *Point) OffsetX (v int) {
	p.x=p.x+v
}
package main 

func main() {
	p:=Point{3,4}
	p.OffsetX(5)
	fmt.Println(p.x)
}

Good Programming Practices

Polymorphism

Inheritance (No Inheritance in GoLang)

Overriding

Interface

Satisfying an Interface

Example


type Shape2D interface {
		Area() float64
		Perimeter() float64
}
type Triangle {...}

func (t Triangle) Area() float64 {....}
func (t Triangle) Perimeter() float64 {....}

Interface vs Concrete Types

Concrete Types

Interface Values

  1. Dynamic Type : Concrete type which it is assigned to
  2. Dynamic Value: Value of the dynamic type

Defining an interface type


type Speaker interface { Speak()}
type Dog struct {name string }
func (d Dog) Speak() {
	fmt.Println(d.name)
}
func main() {
	var s1 Speaker
	var d1 Dog{"Brian"}
    s1=d1
	s1.Speak()
    var s1 Speaker 
    var d1 *Dog 
    s1=d1

Nil Dynamic Value


func (d *Dog)Speak() { 
	if d==nil{
	    fmt.Println("<noise>")
    }else{
	    fmt.Println(d.name)
    }	
}

var s1 Speaker
var d1 *Dog 
s1=d1
s1.Speak() // it works, since s1 is mapped to d1 

Nil Interface Value

Nil dynamic value and valid dynamic type

var s1 Speaker 
var d1 *Dog 
s1=d1 
var s1 Speaker  *(there is no actually method to call)

No dynamic type and no dynamic value then you cannot call the interface…

Using Interfaces

Example Interface for Shapes

Pool in a Yard


type Shape2D interface {
	Area() float64
	Perimeter() float64
}
type Triangle {...}
func (t Triangle) Area() float64 {...}
func (t Triangle) Perimeter() float64 {...}
type Rectangle {...}
func(t Rectangle) Area() float64 {...}
func (t Rectangle) Perimeter() float64 {....}


func FitInYard(s Shape2D) bool {
    if (s.Area() > 100 && s.Perimeter() > 100) {
		return true
    }
return false
} 

Empty Interface

func PrintMe(val interface{} ) {
	fmt.Println(val)
} 

Type Assertions

Concealing Type Differences__

func fitInYard(s Shape2D)bool {
    if (s.Area() >100 && s.Perimeter()>100){
	    return true
    }
    return false
}

Exposing Type Differences

Type Assertions for Disambiguation

func DrawShape(s Shape2D) bool {
	rect,ok :=s.(Rectangle)
	if ok {
		DrawRect(rect)
    }
    tri,ok  := s.(Triangle)
    if ok {
	    DrawRect(tri)
    }
}

Type Switch

func DrawShape(s Shape2D) bool {
    
    switch:= sh:=s.(type) {
	case Rectangle: 
		DrawRect(sh)
	case Triangle: 
		DrawTri(sh) 
    }
}

Error Interface

type error interface {
	Error() string
}

Handling Errors

f,err := os.Open("/harris/text.txt")
if err!=nil {
	fmt.Println(err)
	return 
}

Keep in mind that the topics which are mentioned on this post is just brief summary, it means that all subheaders and headers can be extended to any size, however these are just bulletpoints and overall information in Functions, Methods, and Interfaces in Go module of specialization serie.

In next post, notes which are taken from concurrency module of the specialization serie will be posted.

Take care ! πŸ‘‹πŸ»

Ahmet Turkmen

Ahmet Turkmen

Software Engineer

comments powered by Disqus
rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium cool-kubernetes stackoverflow reddit quora quora dev