@kjk1/

gobook defer basics 2

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8918980676007644","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"Ly8gYWxsb3cgZXJyb3IKcGFja2FnZSBtYWluCgppbXBvcnQgImZtdCIKCi8vIDpzaG93IHN0YXJ0CmZ1bmMgbG9nTnVtKGkgaW50KSB7CglmbXQuUHJpbnRmKCJOdW0gJWRcbiIsIGkpCn0KCmZ1bmMgbWFpbigpIHsKCWRlZmVyIGxvZ051bSgxKQoJZm10LlByaW50bG4oIkZpcnN0IG1haW4gc3RhdGVtZW50IikKCWRlZmVyIGxvZ051bSgyKQoJZGVmZXIgbG9nTnVtKDMpCglwYW5pYygicGFuaWMgb2NjdXJyZWQiKQoKCWZtdC5QcmludGxuKCJMYXN0IG1haW4gc3RhdGVtZW50IikgLy8gbm90IHByaW50ZWQKCgkvLyBub3QgZGVmZXJyZWQgc2luY2UgZXhlY3V0aW9uIGZsb3cgbmV2ZXIgcmVhY2hlcyB0aGlzIGxpbmUKCWRlZmVyIGxvZ051bSgzKQp9CgovLyA6c2hvdyBlbmQ="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// allow error
package main

import "fmt"

// :show start
func logNum(i int) {
	fmt.Printf("Num %d\n", i)
}

func main() {
	defer logNum(1)
	fmt.Println("First main statement")
	defer logNum(2)
	defer logNum(3)
	panic("panic occurred")

	fmt.Println("Last main statement") // not printed

	// not deferred since execution flow never reaches this line
	defer logNum(3)
}

// :show end