From 481091c61f863de7e2494f367efa9c905afe2060 Mon Sep 17 00:00:00 2001 From: kilaka Date: Mon, 15 Feb 2021 10:28:20 +0000 Subject: [PATCH] recover example --- examples.txt | 1 + examples/recover/recover.go | 49 +++++ examples/recover/recover.hash | 2 + examples/recover/recover.sh | 13 ++ public/arrays | 2 +- public/atomic-counters | 2 +- public/base64-encoding | 2 +- public/channel-buffering | 2 +- public/channel-directions | 2 +- public/channel-synchronization | 2 +- public/channels | 2 +- public/closing-channels | 2 +- public/closures | 2 +- public/collection-functions | 4 +- public/command-line-arguments | 2 +- public/command-line-flags | 2 +- public/command-line-subcommands | 2 +- public/constants | 2 +- public/context | 2 +- public/defer | 6 +- public/directories | 2 +- public/environment-variables | 2 +- public/epoch | 2 +- public/errors | 2 +- public/execing-processes | 2 +- public/file-paths | 2 +- public/for | 2 +- public/functions | 2 +- public/goroutines | 2 +- public/http-clients | 2 +- public/http-servers | 2 +- public/if-else | 2 +- public/index.html | 2 + public/interfaces | 2 +- public/json | 2 +- public/line-filters | 2 +- public/maps | 2 +- public/methods | 2 +- public/multiple-return-values | 2 +- public/mutexes | 2 +- public/non-blocking-channel-operations | 2 +- public/number-parsing | 2 +- public/panic | 2 +- public/pointers | 2 +- public/random-numbers | 2 +- public/range | 2 +- public/range-over-channels | 2 +- public/rate-limiting | 2 +- public/reading-files | 2 +- public/recover | 284 +++++++++++++++++++++++++ public/recursion | 2 +- public/regular-expressions | 2 +- public/select | 2 +- public/sha1-hashes | 2 +- public/signals | 2 +- public/slices | 2 +- public/sorting | 2 +- public/sorting-by-functions | 2 +- public/spawning-processes | 2 +- public/stateful-goroutines | 2 +- public/string-formatting | 2 +- public/string-functions | 2 +- public/structs | 2 +- public/switch | 2 +- public/temporary-files-and-directories | 2 +- public/testing | 2 +- public/tickers | 2 +- public/time | 2 +- public/time-formatting-parsing | 2 +- public/timeouts | 2 +- public/timers | 2 +- public/url-parsing | 2 +- public/values | 2 +- public/variables | 2 +- public/variadic-functions | 2 +- public/waitgroups | 2 +- public/worker-pools | 2 +- public/writing-files | 2 +- public/xml | 2 +- 79 files changed, 427 insertions(+), 76 deletions(-) create mode 100644 examples/recover/recover.go create mode 100644 examples/recover/recover.hash create mode 100644 examples/recover/recover.sh create mode 100644 public/recover diff --git a/examples.txt b/examples.txt index 27b9df5..cadc52c 100644 --- a/examples.txt +++ b/examples.txt @@ -41,6 +41,7 @@ Sorting Sorting by Functions Panic Defer +Recover Collection Functions String Functions String Formatting diff --git a/examples/recover/recover.go b/examples/recover/recover.go new file mode 100644 index 0000000..156fb6f --- /dev/null +++ b/examples/recover/recover.go @@ -0,0 +1,49 @@ +// 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. + +package main + +import ( + "fmt" +) + +func main() { + + recoverFromBuiltInPanic(10) + + fmt.Println() + + recoverFromCustomPanic(-1) +} + +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. + defer func() { + if r := recover(); r != nil { + fmt.Println("Recovered. Error:\n", r) + } + }() + + fmt.Printf("About to process i=%d\n", i) + + if i < 0 { + panic(fmt.Errorf("Accepting only"+ + " non-negative numbers but received %d", i)) + } + + fmt.Printf("Doing something with %d\n", i) +} diff --git a/examples/recover/recover.hash b/examples/recover/recover.hash new file mode 100644 index 0000000..11d3657 --- /dev/null +++ b/examples/recover/recover.hash @@ -0,0 +1,2 @@ +a098d6e6717560d0b67ae579b78e00a74cf8781a +uVo4c0IE97q diff --git a/examples/recover/recover.sh b/examples/recover/recover.sh new file mode 100644 index 0000000..aac1d48 --- /dev/null +++ b/examples/recover/recover.sh @@ -0,0 +1,13 @@ +# Running this program will exit correctly, +# even though panic was invoked in two methods. +# The recover is responsible for recovering from panics. +$ 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 +# Note that, in Go it is idiomatic +# to use error-indicating return values wherever possible. diff --git a/public/arrays b/public/arrays index 0a60105..7c116e7 100644 --- a/public/arrays +++ b/public/arrays @@ -206,7 +206,7 @@ typical Go. We’ll look at slices next.

diff --git a/public/atomic-counters b/public/atomic-counters index a6fef35..b70cbc7 100644 --- a/public/atomic-counters +++ b/public/atomic-counters @@ -236,7 +236,7 @@ state.

diff --git a/public/base64-encoding b/public/base64-encoding index 74ffc9a..9b2150b 100644 --- a/public/base64-encoding +++ b/public/base64-encoding @@ -191,7 +191,7 @@ but they both decode to the original string as desired.

diff --git a/public/channel-buffering b/public/channel-buffering index 0bea69d..fe8793b 100644 --- a/public/channel-buffering +++ b/public/channel-buffering @@ -153,7 +153,7 @@ concurrent receive.

diff --git a/public/channel-directions b/public/channel-directions index 2814e81..763ec5b 100644 --- a/public/channel-directions +++ b/public/channel-directions @@ -145,7 +145,7 @@ receive on this channel.

diff --git a/public/channel-synchronization b/public/channel-synchronization index 804f06c..362b430 100644 --- a/public/channel-synchronization +++ b/public/channel-synchronization @@ -184,7 +184,7 @@ started.

diff --git a/public/channels b/public/channels index c3f4a4a..cde4702 100644 --- a/public/channels +++ b/public/channels @@ -168,7 +168,7 @@ message without having to use any other synchronization.

diff --git a/public/closing-channels b/public/closing-channels index cba2538..b1b5c67 100644 --- a/public/closing-channels +++ b/public/closing-channels @@ -194,7 +194,7 @@ example: range over channels.

diff --git a/public/closures b/public/closures index 3ef94ee..acb4359 100644 --- a/public/closures +++ b/public/closures @@ -190,7 +190,7 @@ recursion.

diff --git a/public/collection-functions b/public/collection-functions index 6d7db8c..01af859 100644 --- a/public/collection-functions +++ b/public/collection-functions @@ -9,7 +9,7 @@ onkeydown = (e) => { if (e.key == "ArrowLeft") { - window.location.href = 'defer'; + window.location.href = 'recover'; } @@ -371,7 +371,7 @@ type.

diff --git a/public/command-line-arguments b/public/command-line-arguments index a3f61e4..6c2251c 100644 --- a/public/command-line-arguments +++ b/public/command-line-arguments @@ -170,7 +170,7 @@ with flags.

diff --git a/public/command-line-flags b/public/command-line-flags index b308844..ed2b9a9 100644 --- a/public/command-line-flags +++ b/public/command-line-flags @@ -310,7 +310,7 @@ and show the help text again.

diff --git a/public/command-line-subcommands b/public/command-line-subcommands index 60e9928..d226e51 100644 --- a/public/command-line-subcommands +++ b/public/command-line-subcommands @@ -263,7 +263,7 @@ way to parameterize programs.

diff --git a/public/constants b/public/constants index 737bfc0..8e80c8c 100644 --- a/public/constants +++ b/public/constants @@ -183,7 +183,7 @@ assignment or function call. For example, here diff --git a/public/context b/public/context index d808591..6032419 100644 --- a/public/context +++ b/public/context @@ -205,7 +205,7 @@ cancellation.

diff --git a/public/defer b/public/defer index b15ef1e..8630332 100644 --- a/public/defer +++ b/public/defer @@ -14,7 +14,7 @@ if (e.key == "ArrowRight") { - window.location.href = 'collection-functions'; + window.location.href = 'recover'; } } @@ -203,7 +203,7 @@ after being written.

- Next example: Collection Functions. + Next example: Recover.

diff --git a/public/directories b/public/directories index 0d7d9f5..19a325d 100644 --- a/public/directories +++ b/public/directories @@ -353,7 +353,7 @@ recursively by filepath.Walk.

diff --git a/public/environment-variables b/public/environment-variables index 65b18c9..6015252 100644 --- a/public/environment-variables +++ b/public/environment-variables @@ -186,7 +186,7 @@ program picks that value up.

diff --git a/public/epoch b/public/epoch index 192c24c..5ad5ceb 100644 --- a/public/epoch +++ b/public/epoch @@ -177,7 +177,7 @@ parsing and formatting.

diff --git a/public/errors b/public/errors index be57978..8dce680 100644 --- a/public/errors +++ b/public/errors @@ -299,7 +299,7 @@ on the Go blog for more on error handling.

diff --git a/public/execing-processes b/public/execing-processes index 687da4a..cfa31cf 100644 --- a/public/execing-processes +++ b/public/execing-processes @@ -203,7 +203,7 @@ processes covers most use cases for fork.

diff --git a/public/file-paths b/public/file-paths index 1e5e204..6c1e6f6 100644 --- a/public/file-paths +++ b/public/file-paths @@ -250,7 +250,7 @@ be made relative to base.

diff --git a/public/for b/public/for index e7b756b..c5273e9 100644 --- a/public/for +++ b/public/for @@ -195,7 +195,7 @@ structures.

diff --git a/public/functions b/public/functions index 2747daa..6fd4d89 100644 --- a/public/functions +++ b/public/functions @@ -193,7 +193,7 @@ multiple return values, which we’ll look at next.

diff --git a/public/goroutines b/public/goroutines index 0defc4c..2ebd5dc 100644 --- a/public/goroutines +++ b/public/goroutines @@ -207,7 +207,7 @@ concurrent Go programs: channels.

diff --git a/public/http-clients b/public/http-clients index d3c773d..e7dc068 100644 --- a/public/http-clients +++ b/public/http-clients @@ -169,7 +169,7 @@ settings.

diff --git a/public/http-servers b/public/http-servers index 72b4ef0..797f74e 100644 --- a/public/http-servers +++ b/public/http-servers @@ -210,7 +210,7 @@ router we’ve just set up.

diff --git a/public/if-else b/public/if-else index b599e09..b684ea3 100644 --- a/public/if-else +++ b/public/if-else @@ -184,7 +184,7 @@ for basic conditions.

diff --git a/public/index.html b/public/index.html index 470a699..129e934 100644 --- a/public/index.html +++ b/public/index.html @@ -109,6 +109,8 @@
  • Defer
  • +
  • Recover
  • +
  • Collection Functions
  • String Functions
  • diff --git a/public/interfaces b/public/interfaces index baf76af..ff1626f 100644 --- a/public/interfaces +++ b/public/interfaces @@ -236,7 +236,7 @@ these structs as arguments to measure.

    diff --git a/public/json b/public/json index b39d512..ac13e5e 100644 --- a/public/json +++ b/public/json @@ -416,7 +416,7 @@ for more.

    diff --git a/public/line-filters b/public/line-filters index d7e5fea..34f8e9a 100644 --- a/public/line-filters +++ b/public/line-filters @@ -204,7 +204,7 @@ lowercase lines.

    diff --git a/public/maps b/public/maps index c3bb882..25cd47f 100644 --- a/public/maps +++ b/public/maps @@ -232,7 +232,7 @@ printed with fmt.Println.

    diff --git a/public/methods b/public/methods index b8e0a58..44c275b 100644 --- a/public/methods +++ b/public/methods @@ -197,7 +197,7 @@ naming related sets of methods: interfaces.

    diff --git a/public/multiple-return-values b/public/multiple-return-values index fa3ca35..08ab6eb 100644 --- a/public/multiple-return-values +++ b/public/multiple-return-values @@ -166,7 +166,7 @@ feature of Go functions; we’ll look at this next.

    diff --git a/public/mutexes b/public/mutexes index 94208eb..e97f94b 100644 --- a/public/mutexes +++ b/public/mutexes @@ -297,7 +297,7 @@ management task using only goroutines and channels.

    diff --git a/public/non-blocking-channel-operations b/public/non-blocking-channel-operations index ea30fd1..a874f03 100644 --- a/public/non-blocking-channel-operations +++ b/public/non-blocking-channel-operations @@ -176,7 +176,7 @@ on both messages and signals.

    diff --git a/public/number-parsing b/public/number-parsing index e222bc7..798acfd 100644 --- a/public/number-parsing +++ b/public/number-parsing @@ -213,7 +213,7 @@ bits.

    diff --git a/public/panic b/public/panic index 9242fcc..eb18040 100644 --- a/public/panic +++ b/public/panic @@ -172,7 +172,7 @@ to use error-indicating return values wherever possible.

    diff --git a/public/pointers b/public/pointers index 62d3a70..716f4b3 100644 --- a/public/pointers +++ b/public/pointers @@ -193,7 +193,7 @@ the memory address for that variable.

    diff --git a/public/random-numbers b/public/random-numbers index 39f9246..9bbddb1 100644 --- a/public/random-numbers +++ b/public/random-numbers @@ -229,7 +229,7 @@ that Go can provide.

    diff --git a/public/range b/public/range index 81877c9..593785d 100644 --- a/public/range +++ b/public/range @@ -200,7 +200,7 @@ of the rune and the second the rune itself.

    diff --git a/public/range-over-channels b/public/range-over-channels index 6eedb6a..1a36cc9 100644 --- a/public/range-over-channels +++ b/public/range-over-channels @@ -154,7 +154,7 @@ values be received.

    diff --git a/public/rate-limiting b/public/rate-limiting index 25900dd..06a79ff 100644 --- a/public/rate-limiting +++ b/public/rate-limiting @@ -261,7 +261,7 @@ then serve the remaining 2 with ~200ms delays each.

    diff --git a/public/reading-files b/public/reading-files index abed995..84cd8e4 100644 --- a/public/reading-files +++ b/public/reading-files @@ -287,7 +287,7 @@ be scheduled immediately after Opening with diff --git a/public/recover b/public/recover new file mode 100644 index 0000000..e12ca15 --- /dev/null +++ b/public/recover @@ -0,0 +1,284 @@ + + + + + Go by Example: Recover + + + + +
    +

    Go by Example: Recover

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +

    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.

    + +
    + + +
    + + + +
    package main
    +
    + +
    + + + +
    import (
    +    "fmt"
    +)
    +
    + +
    + + + +
    func main() {
    +
    + +
    + + + +
        recoverFromBuiltInPanic(10)
    +
    + +
    + + + +
        fmt.Println()
    +
    + +
    + + + +
        recoverFromCustomPanic(-1)
    +}
    +
    + +
    +

    defer is defined.

    + +
    + +
    func recoverFromBuiltInPanic(i int) {
    +
    + +
    + + + +
        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 {
    +            fmt.Println("Recovered. Error:\n", r)
    +        }
    +    }()
    +
    + +
    + + + +
        fmt.Printf("About to process i=%d\n", i)
    +
    + +
    + + + +
        if i < 0 {
    +        panic(fmt.Errorf("Accepting only"+
    +            " non-negative numbers but received %d", i))
    +    }
    +
    + +
    + + + +
        fmt.Printf("Doing something with %d\n", i)
    +}
    +
    + +
    + + + + + + + + + + + + + +
    +

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

    + +
    + +
    $ go run recover.go
    +Getting index 10 of array of len 5...
    +Recovered. Error:
    + runtime error: index out of range [10] with length 5
    +
    + +
    +

    Note that, in Go it is idiomatic +to use error-indicating return values wherever possible.

    + +
    + +
    About to process i=-1
    +Recovered. Error:
    + Accepting only non-negative numbers but received -1
    +
    + +
    + + +

    + Next example: Collection Functions. +

    + + +
    + + + + diff --git a/public/recursion b/public/recursion index d1125aa..ed6a80e 100644 --- a/public/recursion +++ b/public/recursion @@ -125,7 +125,7 @@ base case of fact(0).

    diff --git a/public/regular-expressions b/public/regular-expressions index ec33157..4d0a552 100644 --- a/public/regular-expressions +++ b/public/regular-expressions @@ -343,7 +343,7 @@ the regexp package docs diff --git a/public/select b/public/select index c92146f..9173db9 100644 --- a/public/select +++ b/public/select @@ -183,7 +183,7 @@ concurrently.

    diff --git a/public/sha1-hashes b/public/sha1-hashes index 82dfb4f..5951888 100644 --- a/public/sha1-hashes +++ b/public/sha1-hashes @@ -203,7 +203,7 @@ you should carefully research diff --git a/public/signals b/public/signals index af1a001..3d019dd 100644 --- a/public/signals +++ b/public/signals @@ -188,7 +188,7 @@ causing the program to print interrupt and then exit.

    diff --git a/public/slices b/public/slices index 798a0ca..053957e 100644 --- a/public/slices +++ b/public/slices @@ -308,7 +308,7 @@ Go’s other key builtin data structure: maps.

    diff --git a/public/sorting b/public/sorting index e341758..6388d97 100644 --- a/public/sorting +++ b/public/sorting @@ -160,7 +160,7 @@ slices and true as the result of our AreSorted test. diff --git a/public/sorting-by-functions b/public/sorting-by-functions index 4f0f50e..bcac966 100644 --- a/public/sorting-by-functions +++ b/public/sorting-by-functions @@ -177,7 +177,7 @@ functions.

    diff --git a/public/spawning-processes b/public/spawning-processes index 568b7f2..66ed638 100644 --- a/public/spawning-processes +++ b/public/spawning-processes @@ -259,7 +259,7 @@ as if we had run them directly from the command-line.

    diff --git a/public/stateful-goroutines b/public/stateful-goroutines index 88182c5..5799242 100644 --- a/public/stateful-goroutines +++ b/public/stateful-goroutines @@ -312,7 +312,7 @@ program.

    diff --git a/public/string-formatting b/public/string-formatting index 5c1f574..fb1c0eb 100644 --- a/public/string-formatting +++ b/public/string-formatting @@ -457,7 +457,7 @@ and returns a string without printing it anywhere.

    diff --git a/public/string-functions b/public/string-functions index f486976..3674107 100644 --- a/public/string-functions +++ b/public/string-functions @@ -209,7 +209,7 @@ for more information.

    diff --git a/public/structs b/public/structs index 552be5a..93495d0 100644 --- a/public/structs +++ b/public/structs @@ -266,7 +266,7 @@ pointers are automatically dereferenced.

    diff --git a/public/switch b/public/switch index 3cf0fc6..8893bfc 100644 --- a/public/switch +++ b/public/switch @@ -203,7 +203,7 @@ type corresponding to its clause.

    diff --git a/public/temporary-files-and-directories b/public/temporary-files-and-directories index 1110be0..881042c 100644 --- a/public/temporary-files-and-directories +++ b/public/temporary-files-and-directories @@ -242,7 +242,7 @@ prefixing them with our temporary directory.

    diff --git a/public/testing b/public/testing index 673e027..1e18c07 100644 --- a/public/testing +++ b/public/testing @@ -231,7 +231,7 @@ when executing go test -v.

    diff --git a/public/tickers b/public/tickers index aa0fac7..e890ce4 100644 --- a/public/tickers +++ b/public/tickers @@ -171,7 +171,7 @@ before we stop it.

    diff --git a/public/time b/public/time index 246ffa8..b22da8c 100644 --- a/public/time +++ b/public/time @@ -270,7 +270,7 @@ the Unix epoch.

    diff --git a/public/time-formatting-parsing b/public/time-formatting-parsing index 6e7fbcb..18fe74d 100644 --- a/public/time-formatting-parsing +++ b/public/time-formatting-parsing @@ -204,7 +204,7 @@ explaining the parsing problem.

    diff --git a/public/timeouts b/public/timeouts index b4d14e7..8adcac2 100644 --- a/public/timeouts +++ b/public/timeouts @@ -181,7 +181,7 @@ out and the second succeeding.

    diff --git a/public/timers b/public/timers index 4e75368..090da3a 100644 --- a/public/timers +++ b/public/timers @@ -184,7 +184,7 @@ a chance to fire.

    diff --git a/public/url-parsing b/public/url-parsing index cb70ac7..4df2e8a 100644 --- a/public/url-parsing +++ b/public/url-parsing @@ -235,7 +235,7 @@ pieces that we extracted.

    diff --git a/public/values b/public/values index aee60a2..dc3afc2 100644 --- a/public/values +++ b/public/values @@ -152,7 +152,7 @@ basic examples.

    diff --git a/public/variables b/public/variables index 312b5e5..8f4b72f 100644 --- a/public/variables +++ b/public/variables @@ -183,7 +183,7 @@ initializing a variable, e.g. for diff --git a/public/variadic-functions b/public/variadic-functions index 0ce77f5..97e6c65 100644 --- a/public/variadic-functions +++ b/public/variadic-functions @@ -172,7 +172,7 @@ to form closures, which we’ll look at next.

    diff --git a/public/waitgroups b/public/waitgroups index 85856ce..cb6b3dc 100644 --- a/public/waitgroups +++ b/public/waitgroups @@ -229,7 +229,7 @@ is likely to be different for each invocation.

    diff --git a/public/worker-pools b/public/worker-pools index e7fec32..7372dc0 100644 --- a/public/worker-pools +++ b/public/worker-pools @@ -224,7 +224,7 @@ there are 3 workers operating concurrently.

    diff --git a/public/writing-files b/public/writing-files index 751a9e8..fc23770 100644 --- a/public/writing-files +++ b/public/writing-files @@ -289,7 +289,7 @@ we’ve just seen to the stdin and stdout streams. diff --git a/public/xml b/public/xml index 4c7e048..337d722 100644 --- a/public/xml +++ b/public/xml @@ -281,7 +281,7 @@ to nest all plants under <parent><child>...