0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Go deferとpanicと俺

Posted at

package main

import (
	"fmt"
)

func main() {

	defer fmt.Println("①最初に書いたけど、これが最後")
	
	// 関数も指定できるよ
	defer deferFunc()
	
	
	// これは無名関数だよ
	defer func() {
		fmt.Println("③deferって複数指定するとスタックなんだね")
	}()

	// panicから復旧してみるよ
	defer func() {
		e := recover()
		fmt.Println("④recover関数で復旧するよ:" , e)
	}()
	
	panic("ぱにっぱにっ。 ぱにっぱにっ。 ぱにっぱにっぱにっく。")

}

func deferFunc() {
  defer fmt.Println("②2番目に書いたけど、これが①より先")
}

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?