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..7cf7287 --- /dev/null +++ b/examples/recover/recover.go @@ -0,0 +1,41 @@ +// Go makes it possible to _recover_ from a panic, by +// using the `recover` built-in function. A `recover` can +// stop a `panic` from aborting the program and let it +// continue with execution instead. + +// An example of where this can be useful: a server +// wouldn't want to crash if one of the client connections +// exhibits a critical error. Instead, the server would +// want to close that connection and continue serving +// other clients. In fact, this is what Go's `net/http` +// does by default for HTTP servers. + +package main + +import "fmt" + +// This function panics. +func mayPanic() { + panic("a problem") +} + +func main() { + // `recover` must be called within a deferred function. + // When the enclosing function panics, the defer will + // activate and a `recover` call within it will catch + // the panic. + defer func() { + if r := recover(); r != nil { + // The return value of `recover` is the error raised in + // the call to `panic`. + fmt.Println("Recovered. Error:\n", r) + } + }() + + mayPanic() + + // This code will not run, because `mayPanic` panics. + // The execution of `main` stops at the point of the + // panic and resumes in the deferred closure. + fmt.Println("After mayPanic()") +} diff --git a/examples/recover/recover.hash b/examples/recover/recover.hash new file mode 100644 index 0000000..4aa77d5 --- /dev/null +++ b/examples/recover/recover.hash @@ -0,0 +1,2 @@ +053ecbddb4f4a1d881aa40086de99da4e78b9990 +Sk-SVdofEIZ diff --git a/examples/recover/recover.sh b/examples/recover/recover.sh new file mode 100644 index 0000000..df26fc4 --- /dev/null +++ b/examples/recover/recover.sh @@ -0,0 +1,2 @@ +Recovered. Error: + a problem diff --git a/public/collection-functions b/public/collection-functions index 3dc5de0..ae5f9c6 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'; } diff --git a/public/defer b/public/defer index 647f667..00c1a13 100644 --- a/public/defer +++ b/public/defer @@ -14,7 +14,7 @@ if (e.key == "ArrowRight") { - window.location.href = 'collection-functions'; + window.location.href = 'recover'; } } @@ -195,7 +195,7 @@ after being written.

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

  • Defer
  • +
  • Recover
  • +
  • Collection Functions
  • String Functions
  • diff --git a/public/recover b/public/recover new file mode 100644 index 0000000..64cf86a --- /dev/null +++ b/public/recover @@ -0,0 +1,196 @@ + + + + + Go by Example: Recover + + + + +
    +

    Go by Example: Recover

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

    Go makes it possible to recover from a panic, by +using the recover built-in function. A recover can +stop a panic from aborting the program and let it +continue with execution instead.

    + +
    + + +
    +

    An example of where this can be useful: a server +wouldn’t want to crash if one of the client connections +exhibits a critical error. Instead, the server would +want to close that connection and continue serving +other clients. In fact, this is what Go’s net/http +does by default for HTTP servers.

    + +
    + + +
    + + + +
    package main
    +
    +
    + + + +
    import "fmt"
    +
    +
    +

    This function panics.

    + +
    + +
    +func mayPanic() {
    +    panic("a problem")
    +}
    +
    +
    +

    recover must be called within a deferred function. +When the enclosing function panics, the defer will +activate and a recover call within it will catch +the panic.

    + +
    + +
    func main() {
    +
    +
    +

    The return value of recover is the error raised in +the call to panic.

    + +
    + +
        defer func() {
    +        if r := recover(); r != nil {
    +
    +
    + + + +
                fmt.Println("Recovered. Error:\n", r)
    +        }
    +    }()
    +
    +
    + + + +
        mayPanic()
    +
    +
    +

    This code will not run, because mayPanic panics. +The execution of main stops at the point of the +panic and resumes in the deferred closure.

    + +
    + +
    +    fmt.Println("After mayPanic()")
    +}
    +
    +
    + + + + + + + + +
    + + + +
    Recovered. Error:
    + a problem
    +
    + + +

    + Next example: Collection Functions. +

    + + +
    + + + +