News for hackers. Stuff that matters.
Re:The Go Programming Language by Brian W. Kernighan, Alan D... by wallyhs on Thursday March 05, 2015 @07:51PM (#9154921)
Don't forget about the tooling: unit testing, benchmarking, generated documentation, code formatting, and emacs+vim plugins are included in the official package. I think these are often overlooked when describing its benefits. You can jump in and start working right away.

In discussions about error handling in Go, I rarely see mention of panic used for private exception handling. If a module exposes a small surface area, the public entry points can recover from panics and return them as errors. Private functions can then use panic to report errors:

  func PublicEntryPoint(input string) (output []byte, err error) {
    defer func() {
      if e := recover(); e != nil {
        output = nil
        err = e
      }
    }()
    err = nil
    output = privateFunc(input) // no need to check for error here
    return
  }
  func privateFunc(input string) []byte {
    foo := otherPrivateFunc(...) // no need to check for error here
    // ...
  }
  func otherPrivateFunc() int {
    // ...
    panic(fmt.Errorf("Something is wrong"))
  }
I would panic-recover a specific type of error instead of the generic "error" type, but I'm simplifying for the example. Also, it is not recommended to panic across module boundaries.

There are no comments on this comment yet.