Quick actions

cmd+k|ctrl+k

Navigation

Languages

Defer / Defer in depth / Essential Go

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T09:41:36Z

Updated

2019-03-26T09:41:36Z

package main

import "fmt"

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
}
INFO