runtime: avoid heap allocs for plain errors

This commit is contained in:
Jake Bailey
2026-07-27 09:08:47 -07:00
committed by Ron Evans
parent 1d2bc0fd64
commit 206d9ae8e0
17 changed files with 132 additions and 50 deletions
+17
View File
@@ -313,12 +313,29 @@ func recoverMustPanic(name string, f func()) {
f()
}
func recoverRuntimeErrorValue(f func()) {
defer func() {
r := recover()
err, ok := r.(runtime.Error)
if ok {
println(" recovered runtime error:", err.Error())
} else {
println(" failed runtime error:", r)
}
}()
f()
}
// Test recovering from nil map assignment and closed channel send.
func recoverNilMapAndChan() {
recoverMustPanic("nil map", func() {
var m map[string]int
m["x"] = 1
})
recoverRuntimeErrorValue(func() {
var m map[string]int
m["x"] = 1
})
recoverMustPanic("closed chan", func() {
ch := make(chan int)
close(ch)