From 47eed5ec17c1925520196d718befa82e69f5ccbc Mon Sep 17 00:00:00 2001 From: kilaka Date: Mon, 15 Feb 2021 14:45:44 +0000 Subject: [PATCH] Shorten example + some more comments per PR feedback - https://github.com/mmcgrana/gobyexample/pull/344#pullrequestreview-590513160 --- examples/recover/recover.go | 31 +++------- examples/recover/recover.hash | 4 +- examples/recover/recover.sh | 8 +-- public/recover | 108 ++++++---------------------------- 4 files changed, 31 insertions(+), 120 deletions(-) diff --git a/examples/recover/recover.go b/examples/recover/recover.go index 156fb6f..b493cae 100644 --- a/examples/recover/recover.go +++ b/examples/recover/recover.go @@ -1,5 +1,5 @@ -// A `recover` means recovering from a `panic`, either from a "business" or "built-in" panic. -// We want to recover if we want to handle a panic, stopping it from propagating upwards. +// A `recover` means recovering from a `panic`, +// stopping the panic from propagating upwards. package main @@ -8,31 +8,15 @@ import ( ) func main() { + recoverFromPanic(-1) - recoverFromBuiltInPanic(10) - - fmt.Println() - - recoverFromCustomPanic(-1) + // We see it because we recovered from a panic. + fmt.Printf("Finished without panicing.") } -func recoverFromBuiltInPanic(i int) { - // defer is defined. - defer func() { - if r := recover(); r != nil { - fmt.Println("Recovered. Error:\n", r) - } - }() - - var a [5]int - fmt.Printf("Getting index %d"+ - " of array of len %d...\n", i, len(a)) - fmt.Printf("Item in index %d: %d", i, a[i]) -} - -func recoverFromCustomPanic(i int) { - // defer is defined. +func recoverFromPanic(i int) { defer func() { + // recover is always defined in a defer. if r := recover(); r != nil { fmt.Println("Recovered. Error:\n", r) } @@ -45,5 +29,6 @@ func recoverFromCustomPanic(i int) { " non-negative numbers but received %d", i)) } + // We won't see this because we paniced. fmt.Printf("Doing something with %d\n", i) } diff --git a/examples/recover/recover.hash b/examples/recover/recover.hash index 11d3657..6d0d85f 100644 --- a/examples/recover/recover.hash +++ b/examples/recover/recover.hash @@ -1,2 +1,2 @@ -a098d6e6717560d0b67ae579b78e00a74cf8781a -uVo4c0IE97q +8e872b4ab1b23636c5718b557cac09a4c2f10e04 +3JB9O01LGko diff --git a/examples/recover/recover.sh b/examples/recover/recover.sh index aac1d48..f76195e 100644 --- a/examples/recover/recover.sh +++ b/examples/recover/recover.sh @@ -1,13 +1,9 @@ # Running this program will exit correctly, -# even though panic was invoked in two methods. -# The recover is responsible for recovering from panics. +# even though panic was called. $ go run recover.go -Getting index 10 of array of len 5... -Recovered. Error: - runtime error: index out of range [10] with length 5 - About to process i=-1 Recovered. Error: Accepting only non-negative numbers but received -1 +Finished without panicing. # Note that, in Go it is idiomatic # to use error-indicating return values wherever possible. diff --git a/public/recover b/public/recover index e12ca15..9203fac 100644 --- a/public/recover +++ b/public/recover @@ -27,8 +27,8 @@ -

A recover means recovering from a panic, either from a “business” or “built-in” panic.\n -We want to recover if we want to handle a panic, stopping it from propagating upwards.

+

A recover means recovering from a panic, +stopping the panic from propagating upwards.

@@ -42,7 +42,7 @@ We want to recover if we want to handle a panic, stopping it from propagating up - +
package main
 
@@ -70,6 +70,7 @@ We want to recover if we want to handle a panic, stopping it from propagating up
func main() {
+    recoverFromPanic(-1)
 
@@ -77,35 +78,12 @@ We want to recover if we want to handle a panic, stopping it from propagating up - - - - -
    recoverFromBuiltInPanic(10)
-
+

We see it because we recovered from a panic.

- - - - - - -
    fmt.Println()
-
- - - - - - - - - - -
    recoverFromCustomPanic(-1)
+          
    fmt.Printf("Finished without panicing.")
 }
 
@@ -114,12 +92,13 @@ We want to recover if we want to handle a panic, stopping it from propagating up -

defer is defined.

+

recover is always defined in a defer.

-
func recoverFromBuiltInPanic(i int) {
+          
func recoverFromPanic(i int) {
+    defer func() {
 
@@ -131,53 +110,7 @@ We want to recover if we want to handle a panic, stopping it from propagating up -
    defer func() {
-        if r := recover(); r != nil {
-            fmt.Println("Recovered. Error:\n", r)
-        }
-    }()
-
- - - - - - - - - - -
    var a [5]int
-    fmt.Printf("Getting index %d"+
-        " of array of len %d...\n", i, len(a))
-    fmt.Printf("Item in index %d: %d", i, a[i])
-}
-
- - - - - - -

defer is defined.

- - - - -
func recoverFromCustomPanic(i int) {
-
- - - - - - - - - - -
    defer func() {
-        if r := recover(); r != nil {
+          
        if r := recover(); r != nil {
             fmt.Println("Recovered. Error:\n", r)
         }
     }()
@@ -215,7 +148,8 @@ We want to recover if we want to handle a panic, stopping it from propagating up
         
         
           
-            
+            

We won’t see this because we paniced.

+ @@ -233,16 +167,16 @@ We want to recover if we want to handle a panic, stopping it from propagating up

Running this program will exit correctly, -even though panic was invoked in two methods. -The recover is responsible for recovering from panics.

+even though panic was called.

$ go run recover.go
-Getting index 10 of array of len 5...
+About to process i=-1
 Recovered. Error:
- runtime error: index out of range [10] with length 5
+ Accepting only non-negative numbers but received -1
+Finished without panicing.
 
@@ -254,13 +188,9 @@ The recover is responsible for recovering from panics.

to use error-indicating return values wherever possible.

- + -
About to process i=-1
-Recovered. Error:
- Accepting only non-negative numbers but received -1
-
- + @@ -277,7 +207,7 @@ to use error-indicating return values wherever possible.