recover
Calls in the Loop Bodies of Ranging Over Iterators Don't Work
123
.
package main
import "fmt"
func main() {
defer foo()
panic(123)
}
func foo() {
for range 1 {
fmt.Println(recover()) // 123
}
}
123
. But it prints <nil>
then crashes (since Go toolchain version 1.23.0).
package main
import "fmt"
func main() {
defer foo()
panic(123)
}
func foo() {
for range iter {
fmt.Println(recover())
}
}
func iter(yield func() bool) {
yield()
}
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.
recover
calls in the loop bodies of ranging over iterators don't work