Avatar
handle とスコープについては、 Go の defer はスコープ関係なく関数単位だったはずだから、 handle はどうなんだろう?とさっきから思ってる。
https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling.mdEach check may have a different handler chain function depending on the scope in which it is defined. For example, consider this function: がまさにそこに言及していそう func process(user string, files chan string) (n int, err error) { handle err { return 0, fmt.Errorf("process: %v", err) } // handler A for i := 0; i < 3; i++ { handle err { err = fmt.Errorf("attempt %d: %v", i, err) } // handler B handle err { err = moreWrapping(err) } // handler C check do(something()) // check 1: handler chain C, B, A } check do(somethingElse()) // check 2: handler chain A }
(edited)
5:42 AM
Check 1, inside the loop, runs handlers C, B, and A, in that order. Note that because handle is lexically scoped, the handlers defined in the loop body do not accumulate on each new iteration, in contrast to defer.