Value Search

Sunday, November 15, 2009

Go: new open source programming language from Google


 
a systems programming language 
expressive, concurrent, garbage-collected

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.

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:
  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. 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.


For more details, check out the project's official website.

READ MORE