19 Jun 2011

Go: first impressions

After having written about 300 lines of Go code for something meant to be used in the real world, I have mixed feelings about Go.

Things I like
  • Static typing done really well.  Lightweight interfaces (see Rotting Cats section of Go tutorial), type inference, closures, etc. make the language fun like a dynamic language.
  • Clean standard library.  The interfaces are well thought out, and names are picked well.  Effective Go, the document that describes the design/style ideas used by the Go project says "long names don't automatically make things more readable" and I can't agree more.
  • Google App Engine support for Go.  This is the deciding factor for writing my current app in Go.
  • Object oriented programming without fetters.  There are no classes.  There is no type hierarchy.  You can define methods for any type, even those defined by a third party library you don't have source code for.
Things I have to get used to
There are certain things that are not entirely new to me, but I have almost forgotten because I have been coding mostly in Python and Java.
  • Values are different from pointers.  I can define a method on a type like this:
    func (o MyObject) Increment() {
        o.count++
    }
    But this will silently fail because o's type is not *MyObject.  So when I call myo.Increment(), myo is passed by value, i.e., a copy of myo is passed to Increment, not a reference.
  • Crashes and core dumps.  Because the code compiles to native code, you can easily crash your program by dereferencing a null pointer or accessing a nonexistent index in an array.
Things I don't like
  • Errors are returned, not thrown.  In most modern languages your code is mostly free of error-handling because either you want to propagate it to the caller or you can handle them all in one place (with a try-catch).  Go doesn't have exceptions.  So you have to check for errors after calling every function that may return an error.  If you forget to do that, too bad, the error is silently ignored.

No comments:

Post a Comment