LoginSignup
1
1

More than 5 years have passed since last update.

deferについて

Posted at

deferについて知らない事があったので書き留めておきます。
以下を見て知りました。
darshanime/notes

package main

import "fmt"

func trace(s string) string {
    fmt.Println("entering: ", s)
    return s
}

func un(s string) {
    fmt.Println("leaving: ", s)
}

func a() {
    defer un(trace("a"))
    fmt.Println("in a")
}

func b() {
    defer un(trace("b"))
    fmt.Println("in b")
    a()
}

func main() {
    b()
}

結果

C:\Windows\system32\cmd.exe /c ( go run main.go)
entering:  b
in b
entering:  a
in a
leaving:  a
leaving:  b
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1