Explaining Go error handling

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

I recently translated great article — Errors are values by Rob Pike — and we discussed it in our podcast Golangshow (in russian). One thing I was surprised about is that even experienced Go developers sometimes do not understand the core idea of that article.

Looking back, I remember my first impressions when I read it for the first time. It was similar to “It looks like Pike just adds some complexity to what could’ve been solved gracefully with exceptions”. I have never been fond of exceptions, but that’s the first thought I remember. The example in the article was clearly asking for comparison with exceptions’ way to deal with errors and it didn’t look like a winner here.

Still I knew, there must be something more profound in these words — “errors are values”. After all, I was always comfortable with Go errors handling, so I gave some time to myself to absorb the article.

And then I got it.

Go doesn’t want us to treat errors as something different from our main code. Erroneous situation is a first-class citizen in program flow design.

Errors shouldn’t be hidden or ignored in the same way as you don’t hide or ignore any other code. They are part of your logic and code.

Just try to imagine your way of thinking when you deal with usual concepts — values, conditions, loops etc., and apply it to the errors. Errors are the same level entities as the rest of your code. You don’t ignore return values of other types for no reason, right? You don’t ask language to bring special way to handle boolean variables, because “if” is boring. You don’t ask yourself “What should I do, if I don’t know what to do with this slice on this abstraction level?”. You just program the logic you need.

Again, errors are values, and errors’ handling is a normal programming.

Of course, there are always some patterns to deal with errors (like with any other programming conception), but they emerge naturally and fit perfectly in the existing language capabilities.

Let’s try to illustrate it with an example, close enough to that one in the original article. Say, you have a task — “make repetitive writes with io.Writer and calculate number of bytes written, and stop after 1024-th byte”. You start with straightforward approach:

var count, n intn = write(“one”)count += nif count >= 1024 {    return}n = write(“two”)count += nif count >= 1024 {    return}// etc

http://play.golang.org/p/8033Wp9xly

Of course, you instantly see what’s wrong with this code and, following DRY principle, you decide to deduplicate code, moving repeating parts to separate function or closure:

var count intcntWrite := func(s string) {  n := write(s)  count += n  if count >= 1024 {    os.Exit(0)  }}cntWrite(“one”)cntWrite(“two”)cntWrite(“three”)

http://play.golang.org/p/Hd12rk6wNk

Now it’s better, but still not perfect. You still need a closure, which depends on external variable. It also uses os.Exit(), which makes it hardly reusable after first refactoring. We can do better. Let’s see how our thoughts flow — we have a write function, which does something else, except just writing bytes and we need it to be reusable and isolated entity. Let’s refactor our code:

type cntWriter struct {    count int    w io.Writer}func (cw *cntWriter) write(s string) {    if cw.count >= 1024 {        return     }    n := write(s)    cw.count += n}func (cw *cntWriter) written() int { return cw.count }func main() {    cw := &cntWriter{}    cw.write(“one”)    cw.write(“two”)    cw.write(“three”)    fmt.Printf(“Written %d bytes\n”, cw.written())}

http://play.golang.org/p/66Xd1fD8II

Now it looks much better, we can reuse this custom writer in other functions, it’s isolated and easy to test.

Now, just replace ‘counter’ with ‘error value’ and you’ll get almost the same example as in original article about error handling. But take a note how easy and logical was your flow of thoughts towards this code. You wasn’t distracted by looking for the special counting/passing features of the language. You simply was implementing the logic you need in the best possible way.

This idea is profound enough and could be hard to grasp, especially with the mindset focused on The Only Right Way To Handle Errors. It definitely takes some time to absorb.

Of course, it’s debatable, and I can find both a lot of pros and cons for this approach, as well as for others. We’re not in the black&white world, but Go approach to errors is kind of mature and fresh at the same time, it extremely simple and hard to understand at the same time, it requires some rethinking and effort to get the idea. But, what is more important, it works great in practice.

And once you get it, you stop fight the language. You stop looking for special ways to handle or hide errors. Go makes you respect errors as any other part of your program. You just handle them, without expecting language do the magic for you. In the long run, your code becomes better, even if do not realize it yet.

Now, come and read this article again —  Errors are values — and try to get the gist of it with this perspective.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.