logoalt Hacker News

catlifeonmarslast Wednesday at 2:39 AM1 replyview on HN

How do you feel about

    v, err := foo.Open()
    // …
    defer func() {
        if closeErr := v.Close(); closeErr != nil {
            err = fmt.Errorf("while closing %w: %v", err, closeErr)
        }
    }() 
  
    // …

When you’re writing something trivial/pure, Go’s error handling is fine, if maybe a tad bit verbose, but it quickly becomes nightmarish when you start to do nontrivial things that are typical for systems programming.

FWIW I love Go, it’s my daily driver for most things. I still think it can get messy far too quickly


Replies

database64128last Wednesday at 7:15 AM

Just write `defer v.Close()`? In almost all cases, `close(2)` errors can be safely ignored. Rust also does this: https://github.com/rust-lang/rust/blob/792fc2b033aea7ea7b766...

show 1 reply