A first look at Google Go – Google’s new programming language.
I took advantage of a recent sick weekend, zonked out on my comfy couch, to take a gander at Google’s new Go programming language.
My conclusion – awesome. Google have nothing short of blown apart what I call the cost/benefit curve of language selection.
ANOTHER Language???
Yes – i know – I’m over this language proliferation too, but hear me out – its new and it isn’t. Sort of.
The beauty of Go is that it’s managed to meld the expressiveness and safety of a higher level language, with the lean runtime profile of C.
Even though it was originally intended as a systems level language to replace say C and C++ in writing things like web servers at google (that is to say – not intended for general purpose programming), it’s gaining quite some traction higher up the chain, seeing some prominent ports from Ruby and Python to take advantage of the blazing speed. And the price you’d normally pay in ease of development, language expressiveness, safety and so forth has been shrunk considerably or even wiped out altogether (that’s obviously a subjective judgement but one with which I definitely concur – it just ‘feels’ really productive).
What? Why?
What is Go? Well, loosely, i would characterise it as some sort of a ‘javafication’ of C.
Now please don’t start furiously typing your objections, its a LOOSE analogy, i’ll admit.
What i mean is: it’s been safety-fied the same way java was – most notably via a safe builtin string class, banned pointer manipulation, and it has garbage collection. It’s also got higher level OO features, as well as baked-in concurrency. ie everything you’d generally need to assemble anyway before you start writing code for modern use case scenarios.
Written by some very experienced guys like Rob Pike who have been around the block and know the pain points of the existing systems languages, I think they have done a superb job in formulating Go.
Anyone who’s spent a lot of time in the plush and safe world of higher level protected languages lets off a groan when they have to duck down into something like C. You get the blazing speed but it aint fun remembering to do all the manual housekeeping. Don’t want to get into a debate about this – but there IS a reason we moved on from C. On the flip side, there’s also a reason operating systems, Microsoft Office and Adobe Photoshop aint yet written in Java, C#, Ruby or Python. And right there is the justification for something like Google Go.
In the multi-cored, concurrent world we now live in, tracking who owns what object as it gets passed between multiple threads in your eight core CPU become a problem too complex for mere humans to handle. And if you get it wrong the errors are always intermittent and difficult to track down. hence Pike’s insistance that garbage collection is a necessity for this new language. I agree. Same with pointer manipulation – the price paid is too high for the performance gain on top of indexed addressing.
Those worried about performance – chill. Go still feels and compiles like low level C. Unlike higher level languages stack allocation is still used far more frequently for short lived objects compared to something like the JVM. Stack allocated objects aren’t managed by the garbage collector (they’re cleared for free every time a function exits), so even though Go currently uses a fairly rudimentary mark-sweep collector, it will be taxed a lot less than heap-only allocated code such as Java. Work is currently underway to introduce a more advanced collector, and the team feels that a fully parralel non blocking collector is a possibility. I don’t hear any great complaints about the existing regime though.
Its a mistake to think of this as some sort of a virtual machine architecure. What comes out of the compiler is your native code statically linked to a minimal set of runtime services to stop you from shooting yourself in the foot (or if you find that insulting, to stop lesser programmers shooting YOU in a somewhat more intimate body part). The resource usage profile of your app is going to mirror that of a traditional C application – though the collector and concurrency features obviously take up some space. Even so – how refreshing to be able to do something useful in a megabyte. Takes you back to simpler times!
Go is currently clocked to run about 90% the speed of native C for real code. I’ll gladly take that hit for the productivity and safety benefits. I’m actually having trouble getting good performance stats on my quad core MacBook Pro – the testing tools I use simply swamp the available CPU before the Go based web service I’m testing even gets to a trot. Now testing always has some insertion loss when doing it on the same machine, but I’ve never quite seen such extreme asymmetric behaviour before. Needless to say – Go is freakishly FAST. Those stories you’ve heard about one Go server replacing 20 dynamically scripted servers for the same ported code are true.
As usual, any new language gets the barage of “why wasn’t X included” questions. None from me – i think they’ve made a pragmatic 1.0 release considering what they were trying to achieve.
What else?
Some other points:
* No forward definition of program artifacts. Forget headers files. Declaration and definition are same thing like in most higher level languages.
* Good type inference in source. No ‘stuttering’ – by which i mean Foo foo=new Foo(Foo.X) – boy does that get tiring…
* Flexible but not over the top object system. Yes Go is OO, but it doesnt seem to get in the way. For example it has interfaces, but implementations are duck typed – theres no EXTENDS keyword. A class implements and interface automatically by virtue of the fact it implements the methods defined in the interface. This is all validated during compilation, and in practice seems to work well, reducing unnecessary dependencies between modules. There are a few other funky OO features.
* Modularisation and namespacing
* The inefficiencies of compilation have been entirely removed. No more one hour full builds – more like millisecond incrementals and maybe seconds for full. To give you an idea – the ENTIRE standard library builds from nothing in something like 6 seconds on your laptop. Awesome. Read up on specifics, but some of the reasons are no transitory dependencies through classes, no multiple scanning of header files.
* Well thought out, out of the box concurrency mechanism. Using a concept of channels (strongly typed pipes) that allow inter ‘thread’ communication. The reason i quoted ‘thread’ is because Go defines a much lighter minimal concurrency artifact below that of a thread – called a goroutine. A goroutine is to threads what non blocking stream pipelines are to sockets. And they exist for the same reason. Multiplexing all units of work on a per-thread basis uses too much memory and involves too much overhead, exactly the same way a socket-per-thread blocking IO architecture does. The Go runtime multiplexes goroutines over a small pool of threads, using things like stack segmentation to minimse resource use. You can ignore all this, but the end result is ultra-concurrency (tens/hundreds of thousands of independent threads of control) with efficient resource use. Its got a Grand Central Dispatch type feel about it.
Anyway heres a quick intro to Go concurrency:
sequential:
myfunc()
parallel:
go myfunc()
easy huh? It doesn’t get THAT much more complex. Theres more of an emphasis on message passing (through channels) rather than manual lock manipulation – as there should be.
* Good standard library to get you going fast for common contemporary usage scenarios – high performance REST server? No problem at all – out of the box with standard library.
*Everything statically linked to a native executable WITH NO DEPENDENCIES!
Summary
And that’s why i characterise Go as the systems language for the cloud era. Out of the box, with the standard libarary, it gives you pretty much everythign you need to do great stuff on modern hardware using modern paradigms (yeah i hate that word too – too busy to search for a synonym).
Go is already being lauded from fields the original designers were not intending it for, and they have been pleasantly surprised that they have exceeded their brief.
Go is, surprisingly, being adapted as a general purpose language rather than a systems language.
Congrats to the Google Go team! I expect Go is going to gain quite a following and will be around for the long haul.
Those (like me!) sufering from language fatigue, wondering why expend the energy to consider ANOTHER programming language – i hope i’ve piqued your interest in having a look at it. Go is something special – a language that may stem or reverse the creep towards dynamic languages.
I wouldn’t be surprised to see an option to allow Android apps to be written in Go in the not too distant future.
Go project page - http://golang.org/
rudi…