@kjk1/

gobook defer basics 4

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

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

import "fmt"

// :show start

func plusOne(i int) (result int) {
	// anonymous function must be called by adding ()
	defer func() { result++ }()

	// i is returned as result, which is updated by deferred function above
	// after execution of below return
	return i
}

func main() {
	fmt.Println(plusOne(1)) // 2
}

// :show end