You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For a real-world example of panic and recover, see the json package from the Go standard library. It decodes JSON-encoded data with a set of recursive functions. When malformed JSON is encountered, the parser calls panic to unwind the stack to the top-level function call, which recovers from the panic and returns an appropriate error value (see the 'error' and 'unmarshal' methods of the decodeState type in decode.go).
The text was updated successfully, but these errors were encountered:
nhooyr
changed the title
remove panic and recover example from panic recover defer blog
remove panic and recover example from panic recover defer blog entry
Aug 4, 2018
If the decision is to replace that reference with some other "real world" example, my vote is to have the link include the commit hash. But I don't think we should reference real world code at all since referencing old code can be confusing when someone tries to find that code in master.
It seems that json was switched away from a panic pattern for performance reasons. That doesn't invalidate the use of panic/recover as a pattern within a single package. There are cases where performance is no concern and the panic/recover pattern really is cleaner (and easier to reason about).
I agree with @meirf to just change the URL to be a permalink to a prior version of json.
FiloSottile
changed the title
remove panic and recover example from panic recover defer blog entry
blog: remove panic and recover example from panic recover defer entry
Aug 6, 2018
@dsnet when would it be cleaner? It hides the fact that the internal functions can all return errors. This forced explicit error handling to make the code flow clear is one of the main reasons Go doesn't use exceptions.
At https://blog.golang.org/defer-panic-and-recover
However, this is a poor example and a misuse of panic and recover as exceptions. See https://golang.org/doc/effective_go.html#panic
The example will be invalid soon with the release of go 1.11 as well. The panic/recover was removed from the unmarshal code. See master's
go/src/encoding/json/decode.go
Lines 170 to 185 in 65fa2b6
go/src/encoding/json/decode.go
Lines 171 to 191 in 4c77cb3
Interesting and relevant article: https://about.sourcegraph.com/blog/go-when-is-it-ok-to-recover/
The text was updated successfully, but these errors were encountered: