4 ms·
Interesting, now all you need to do is add exceptions :)
by tonyedgecombe 8y ago
Interesting, now all you need to do is add exceptions :)
- jitl 8y agoGolang already has an exceptions-equivalent construct in the shape of panic() and recover(): https://blog.golang.org/defer-panic-and-recover https://blog.golang.org/defer-panic-and-recover You’re free to write exception-full code using those constructs, although the community tends to use error return values for the most part.
- bb88 8y agoThis is how I understood the use for panics: https://stackoverflow.com/questions/44504354/should-i-use-panic-or-return-error https://stackoverflow.com/questions/44504354/should-i-use-pa... > You should assume that a panic will be immediately fatal, for > the entire program, or at the very least for the current > goroutine. Ask yourself "when this happens, should > the application immediately crash?" If yes, use a panic; > otherwise, use an error. In Java, say, exceptions are the standard for raising a normal error. A class of those exceptions are runtime errors which are equivalent of "panics". Yes you can handle them, but they denote a problem with the program that can't be solved by the interpreter (divide by zero error, etc)
- ithkuil 8y agoIf want (and if it makes sense) you can use panics as a control flow mechanism to quickly bubble up errors inside your implementations. See an example in the standard library itself: https://golang.org/src/encoding/json/encode.go https://golang.org/src/encoding/json/encode.go, line 295 What's frowned upon is leaking this out of your package's interface/contract.
- bb88 8y agoThen it strikes me kind of like C++ exceptions. They have them, and it's kinda supported, and there are certain things you shouldn't do with them, so everyone just uses return codes. Java/Python encourage the use of exceptions as returning error conditions, rather than return error codes.
- IshKebab 8y agopanic() and recover() are not meant to be used like Java or C++ exceptions.
- vorg 8y agoThe phrase "are able to be" is more effectual than the phrase "are not meant to be" when it comes to how people use programming languages in the real world. If it's possible to build a Go system using only panic/recover and defer, just as it is using only interface{}, then there's some people who will use them that way.
- oldgun 8y ago... and class inheritance, namespaces, smart pointers, move semantics, std::initializer_list, etc.. :)
- reificator 8y agoPerhaps. But if Go itself adds exceptions they're going to have to catch a MassDeveloperExodusException. The standard reason I see for people wanting exceptions in Go is because they're sick of writing the following: ```go thing, err := things.New() if err != nil { log.Fatalln(err.Error()) // Or `return err` } ``` But I would argue that it means they're not writing idiomatic go. A good reference for the value of error values is this go blogpost[0]. The HN discussion[1] of that post was also interesting, I like this comment in particular: > In Go I not infrequently make use of a non-nil value AND a non-nil error. A canonical example of this is the io.Reader interface in Go's standard library [1]. I think it is a very useful idiom particularly when dealing with things where failure is more normal - e.g. dealing with network services, disk IO, decoding partially corrupt exif data from images. Often you want a best-effort at the value even if you run into problems. Handling every error from every function that returns an error can be verbose. But for that verbosity you get a much easier to understand failure model, and the tools (defer) to handle failures reliably no matter what comes up. Exceptions are part of the reason that RAII is so critical in the C++ world. [0]: https://blog.golang.org/errors-are-values https://blog.golang.org/errors-are-values [1]: https://news.ycombinator.com/item?id=8877502 https://news.ycombinator.com/item?id=8877502
- gnode 8y agoOn HN, you can quote code by prepending each line with four spaces, but there is no syntax highlighting.
- reificator 8y agoThanks for the tip, but I avoid that because it ruins readability on mobile IMO.
- HeyImAlex 8y agoEh, rust’s result type has shown that you can do the same thing in way that is more type safe and more ergonomic.
- reificator 8y ago