Help us understand the problem. What is going on with this article?

deferが呼ばれるのはreturn前

More than 1 year has passed since last update.
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

参考

adad
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away