Go defer

Go defer

June 19, 2020 | go, programming, cs

tags
Go Programming Programming
import "fmt"

func trace(name string) string {
	fmt.Println("Entering ", name)
	return name
}

func un(name string) string {
	fmt.Println("Exiting", name)
	return name
}

func a() {
	defer un(trace("a"))
	fmt.Println("Hello, world")
}

func b(){
	defer un(trace("b"))
	fmt.Println("Before i < 10 loop")
	for i:=0 ; i < 10; i++{
		defer fmt.Println("i =", i)
	}

	fmt.Println("After i < 10 loop")
}

func main(){
	a()
	b()
}
Entering  a
Hello, world
Exiting a
Entering  b
Before i < 10 loop
After i < 10 loop
i = 9
i = 8
i = 7
i = 6
i = 5
i = 4
i = 3
i = 2
i = 1
i = 0
Exiting b


No notes link to this note

Go to random page