April 25, 2012 at 10:19 pm
filed under Coding

So what languages do I think offer the best prospects for high-productivity coding buttressed by strong typing? Maybe it says something about what my opinion is worth that I’m not so sure about one of the two languages I was putting forward.

Still, this is as good as time as any to talk about the first one. Lately I’ve spent some time with the Go programming language.


If I had to sum up Go in my own words, I’d call it the spiritual successor to C, with modern sensibilities. It has concise and lightweight syntax. It’s memory safe (read: garbage-collected) and strongly typed. It has powerful concurrency primitives at the language level. It has a unique approach to interfaces where they are actually orthogonal to data types, not unlike Haskell’s typeclasses. It has first class functions and lexical closures. And it has a rich standard library.

Although Go doesn’t ship with a REPL or interpreter, there are some projects out there. However, there’s also As far as I’m concerned, this is close enough to a REPL that I’ve found it extremely useful for prototyping, testing out a package from the Go standard library, and I’ve even written code I’ve ended up keeping later.

Go threads the needle between providing a strongly typed language which is more productive than C++ and orders of magnitude less verbose and clunky than Java. It’s more performant than your average scripting language, yet the standard library is just as rich and easy to use. I would probably not go back to Python except for ~100 line helpers, or glue code which is just above shell scripts in terms of complexity.

It’s tough to pick good examples for some rough comparison that aren’t already covered in the Go docs. But here’s one of my hobby-horses, something which is easy in some scripting languages.

Processing command output

This is trivially easy in shell scripts, and shell scripts were made for processing text. Perl makes it approximately as easy, and as Ruby borrows so much from Perl, it’s similarly easy.

In Python 2.x, it’s terrible. I’d rather not write this in C++ or Java.

Here’s how you run a simple command in Go and capture its output:

// exec lives under the os/exec package
out, err := exec.Command("foo", "--bar", "baz").Output()

out is the output, an array of bytes. You’ll want to split it in lines and iterate over it, treating it as (say) an id and a username along with it.

lines := bytes.Split(out, '\n')
for _, line := range lines {
  // Split by spaces.
  fields := bytes.Fields(line)
  fmt.Println("id is %v. username is %v.", fields[0], fields[1])

Notice the lack of type declarations. Most of the time you use := to declare and initialize at once, which ends up feeling a lot like Python or Ruby. The difference is that the Go compiler infers types based on return values and the like, granting the benefit of type checking.

Further reading

I could come up with more examples. I’m trying not to! That’s because you’re better off playing with something like the Go tour. Trust me: you might think it a bit silly at first, but there are exercises and the app will test your solution. It’s very good.

The Go site is rather good, as well. There’s [a Go tutorial] wherein you write a wiki which is worth your time. You should read Effective Go if you’ve decided you’re actually serious about it.

%d bloggers like this: