Panic's  & OOP Golang

Outline

  • What panic is ?

  • Avoid Panic
  • Try Catch
  • Try Catch Case
  • OOP Golang

What Panic Is ?

Panic is a built-in function that stops the ordinary flow of control and begins panicking

Example 1

Example 2

package main

import (
    "fmt"
)

func Div(num, divisor int) int {
    return num/divisor
}

func main() {
    a := 10
    b := 0
    fmt.Println(Div(a,b))
}
package main

import (
    "fmt"
)

type Person struct {
    Name string
    Age int
}

func GetName(p *Person) string {
    return p.Name
}

func main() {	 
    fmt.Println(GetName(nil))
}

Panic Result

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0xffffffff addr=0x0 pc=0xc64da]

goroutine 1 [running]:
main.main()
	/tmp/sandbox914181347/main.go:18 +0x1a

Avoid Panic

  • Write an Overestimate Code
  • Use Recover

Write an Overestimate Code

package main

import (
	"fmt"
)

type Person struct {
    Name string
    Age int
}

func Div(p *Person, divisor int) int {
    if divisor == 0 {
        return 0
    }
    return p.Age/divisor
}

func main() {
    p := &Person{}
    p = nil
    fmt.Println(Div(p,3))
}

Use Recover

package main

import (
	"fmt"
)

type Person struct {
    Name string
    Age int
}

func Div(p *Person, divisor int) int {
    return p.Age/divisor
}

func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered in f", r)
        }
    }()
    p := &Person{}
    p = nil
    fmt.Println(Div(p,3))
}

Try Catch

Try Catch Taste on Golang 

package main

import (
    "log"
    "fmt"
)

type Block struct {
	Try     func()
	Catch   func(Exception)
	Finally func()
}

type Exception interface{}

func (b Block) Do() {
	if b.Finally != nil {
		defer b.Finally()
	}
	if b.Catch != nil {
		defer func() {
			if r := recover(); r != nil {
				b.Catch(r)
			}
		}()
	}
	b.Try()
}


func main () {
    res := 0
    Block {
        Try: func() {
            a := 0
            b := 1
            res = b/a    
        },
        Catch: func(e Exception) {
            log.Println(e)  
        },
        Finaly: func() {
            fmt.Println("finished")
        },
    }.Do()
    fmt.Println(res)
}

Try Catch Case

  • When use other library ( import from someone git )
  • there is a Block in function that optional, but there is a risk that block make panic
  • There is unknown panic might happened when create a function 

OOP Golang

  • OOP like on Golang 

Class => Struct

package main

import (
    "fmt"
)

type Person struct {
    name string
}

func (p Person) GetName() string {
    return p.name
}

func main() {
    p := Person{name:"hamdi"}
    fmt.Println(p.GetName())
}

Encapsulation

package main

import (
    "fmt"
)

// public
func GetName() string {
    return "hamdi"
}

// private
func updateName(s *string) {
    s = "hamdi"
}

Abstrct ( struct ) ?

package main

import (
    "fmt"
)

type Animal interface {
    Walk() int
    Talk() string
}

type Cow struct {
    Leg int
    Weight int
}

type Snake struct {
    Poison bool
    Tall int
}

func (c Cow) Walk() int {
    return 1
}

func (s Snake) Walk() int {
    return 3
}

func (s Snake) Talk() string {
    return "Tsah"
}

func (c Cow) Talk() string {
    return "Mooo"
}

func (c Cow) Run() int {
    return "10"
}

Return abstrct ( struct ) ?

package main

import (
    "fmt"
)

type Animal interface {
    Walk() int
    Talk() string
}

type Cow struct {
    Leg int
    Weight int
}

type Snake struct {
    Poison bool
    Tall int
}

func GetAnimal(s string) Animal {
    if s == "mamal" {
        return Cow{}    
    }
    return Snake{}
}

Inheritance ?

package main

import (
    "fmt"
)

type Car struct {
    
}

func (c Car) GetMaxSpeed() int {
    return 120
}

func (c Car) GetWheels() int {
    return 4
}

type ContainerTruck struct {
    Car
    Type string
}

func (c ContainerTruck) GetWheels() {
    return 6
}

type SportCar struct {
    Car
    Type string
}

func (s SportCar) GetMaxSpeed() int {
    return 200
} 
Made with Slides.com