LoginSignup
0
0

More than 3 years have passed since last update.

deferが呼ばれるのはreturn前

Posted at
package main

import (
        crand "crypto/rand"
        "fmt"
        "github.com/seehuhn/mt19937"
        "math"
        "math/big"
        "math/rand"
        "os"
)

func main() {
        os.Exit(realMain())
}

func realMain() int {
        seed, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
        rng := rand.New(mt19937.New())
        rng.Seed(seed.Int64())
        r := rng.Intn(2)
        defer fmt.Printf("1\n")
        if r == 0 {
                return 1
        }
        defer fmt.Printf("2\n")

        return 0
}

実行結果

$ go run main.go
2
1
$ go run main.go
1
exit status 1

参考

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