Panic


In many other programming languages (e.g Python, Java) if the code encounters a problem it raises an exception that if not treated properly will stop the execution. In some other programming languages (e.g. C, Perl) functions return an error code when something does not work.
Go leans towards the languages that return error, though it usually returns it as a separate value. In addition Go has a concept similar to exceptions, but is rarely used and to reflect the different usage it also has a different name. It is called panic.
In Go if you try to open a file and you fail, this is considered as a normal situation - not an exceptional one, hence Go will return a representation of the error but won't try to stop the program. On the other hand if you try to divide by 0, Go will freak out and panic.
Let's see how does that work.

examples/go-panic/go_panic.go
package main

import (
    "fmt"
)

func main() {
    fmt.Println("before")

    x := div(6, 2)
    fmt.Println(x)

    fmt.Println("middle")

    y := div(6, 0)
    fmt.Println(y)

    fmt.Println("after")
}

func div(a, b int) int {
    c := a / b
    return c
}

before
3
middle
panic: runtime error: integer divide by zero

goroutine 1 [running]:
main.div(...)
	/home/gabor/work/slides/golang/examples/go-panic/go_panic.go:22
main.main()
	/home/gabor/work/slides/golang/examples/go-panic/go_panic.go:15 +0x13a
exit status 2