mchirico / zDaily

Playground to hack and test ideas with Zoe
1 stars 2 forks source link

Day 38: Practice #42

Open mchirico opened 3 years ago

mchirico commented 3 years ago

video

My example program

tacomonkautobot[bot] commented 3 years ago

mchirico, Thanks for opening this issue!

ZoeChiri commented 3 years ago

package main

import "os"

func main() {

    panic("panic")

    _, err := os.Create("/tmp/file")
    if err != nil {
        panic(err)
    }
}
ZoeChiri commented 3 years ago

package main

import (
    "fmt"
    "os"
)

func main() {

    defer fmt.Println("defer")

    os.Exit(2)
}
mchirico commented 3 years ago

os.Exit will prevent unwinding of defer

However, you can use panic..

package main

import (
    "log"
)

func Talk() {
    log.Printf("here:\n")
    panic("panic")
}

func main() {

    defer log.Printf("Bye")
    Talk()

}

Output:

2020/11/19 21:49:12 here:
2020/11/19 21:49:12 Bye
panic: panic