Thursday, December 10, 2009

Go: new open source programming language from Google

Go: new open source programming language from Google

Go is a new programming language from Google that aims for performance that is nearly comparable to C, but with more expressive syntax and faster compilation. What it won't do, however, is liberate the coding masses from bracist tyranny. Google's Go is yet another take on C.

Every computer programmer has a copious pile of opinions about how their programming language of choice could be improved. Who doesn't want more syntactic sugar, better runtime performance, and faster compilation? That's one of the reasons why there are so many programming languages. Creating a compiler is practically a rite of passage for computer science students, and half of the top vendors in the software industry eventually make their own programming language or extend an existing one to the point where it's marginally recognizable.

Despite the large amount of enthusiasm for language design, modern mainstream programming languages don't fall far from the C tree. The best that Microsoft, Sun, and Apple have to offer are just variations on that theme, with the addition of predictable object models and conveniences like garbage collection. The slim minority of language geeks who have rebelled against bracist tyranny and stumbled over to innovative languages like Haskell and Erlang are doomed to toil in relative obscurity.

Are there any influential software vendors who have the vision and leverage to liberate the programming masses from the tiresome anachronisms of C's long legacy? When I learned that Google was going to announce a new programming language, I was hopeful that the search giant would bring something truly novel to the table. They haven't, but the result isn't bad. Although Google's new Go programming language is yet another take on object-oriented C, it's got some nice features.

Go offers an expressive type system, fast compilation, good performance, and built-in language features that simplify threaded programming and concurrency. The language has been under development for roughly two years. It started out as a 20 percent project—time that Google's engineers are given to use as they choose for undirected experimentation—and evolved into a serious full-time undertaking. Google is releasing the source code under the BSD license with the hope that a community will emerge around the new programming language and participate in the effort to make it a compelling choice for software development.

We discussed the new programming language with Google engineer Rob Pike. He says that the Go language itself and the current implementation are relatively mature, but it's not quite ready for adoption in production environments. The ecosystem around the programming language is still a work in progress. There is no IDE integration, the standard libraries are a bit thin, and there aren't a whole lot of real-world code examples yet. Opening up Go to the broader programming community could help to vastly accelerate its advancement in all of those critical areas.

The native Go compilers, called 6g and 8g (for 64-bit and x86), are designed to be extremely fast. There is also an alternative compiler called Gccgo that is based on the GNU Compiler Collection (GCC). The GCC-based compiler isn't as fast but is said to generate more efficient code. I was initially a bit surprised that Google chose not to use the Low-Level Virtual Machine (LLVM) compiler framework—it has a lot of LLVM expertise internally and is using it extensively for their awesome Python optimization effort. Pike says that LLVM was considered during the early stages of the Go project, but its compile-time performance was judged to be inadequate.

The compiled executables are completely native binaries, so it's not like a managed code language where the compiler generates bytecode for a virtual machine. Go does, however, have some runtime components that get embedded in the executables. Actual execution performance is said to be comparable to that of native C.

Some of Google's sample Go code reveals that the syntax is C-like and encourages a conventional imperative programming style. There are functions, "for" loops, standard conditional expressions, and many other features that you'd expect to find in a C-like language, but with a handful of nice twists. For example, there is a shorthand syntax for variable assignment that supports simple type inference. It also has anonymous function syntax that lets you use real closures. There are some Python-like features too, including array slices and a map type with constructor syntax that looks like Python's dictionary concept. The following code snippet is an example from Google's documentation:
view plainprint?

1. func sum(a []int) int { // returns an int
2. s := 0;
3. for i := 0; i < len(a); i++ {
4. s += a[i]
5. }
6. return s
7. }
8.
9. s := sum(&[3]int{1,2,3}); // a slice of the array is passed to sum

func sum(a []int) int { // returns an int
s := 0;
for i := 0; i < len(a); i++ {
s += a[i]
}
return s
}

s := sum(&[3]int{1,2,3}); // a slice of the array is passed to sum

One of the distinguishing characteristics of Go is its unusual type system. It eschews some typical object-oriented programming concepts such as inheritance. You can define struct types and then create methods for operating on them. You can also define interfaces, much like you can in Java. In Go, however, you don't manually specify which interface a class implements. Pike explained to me that the interface mechanism gives developers some of the flexibility of duck-typing, but it goes further by providing the advantages of compile-time checking.

Parallelism is emphasized in Go's design. The language introduces the concept of "goroutines" which are executed concurrently. Any function can be executed as a goroutine by prefixing the function call with the "go" keyword. The language provides a "channel" mechanism that can be used to safely pass data in and out of goroutines.

Why create a new programming language from scratch? According to Pike, the developers behind the Go project felt that there was a need for a fresh start. It wasn't enough to just add features to existing programming languages, because sometimes you can get more in the long run by taking things away. They wanted to start from scratch and rethink everything. I'm all for a clean break, but the C-like syntax seems to run counter to that goal. He explained that Go is more radical than it looks, especially when you start to look closely at the power of its unusual type system. Pike and his colleagues didn't want to deviate too much from what developers already knew because they wanted to avoid alienating Go's target audience.

As a demonstration of the language, the developers have used it to build the Web server software that powers the project's official website. Aside from that, Go isn't used in production anywhere at Google yet. It's going to take time before it's ready for more expansive use, but Google hopes that it will eventually provide a good alternative to the C++ the company uses extensively. For more details, check out the project's official website.

No comments: