S G Ganesh, Member Technical Staff, Siemens Corporate Technologies & Author
Go is a new, experimental, concurrent, and garbage-collected systems
programming language. It is at experimental stage with tools, packages etc.
still in development. There is no production system implemented in Go as yet.
It is a concurrent language which supports 'communication channels' which is
based on Hoare's Communicating Sequential Processes (CSP). The memory is
automatically garbage collected, which relieves of the programmer from manual
bookkeeping of memory (as done in C/C++). It is intended for writing things like
web servers. Still, we can use it as a general purpose language. Robert
Griesemer, Ken Thompson (of Unix fame), and Rob Pike are the creators of the
language. All of Go's toolset (compilers, packages, runtime libraries, tools
etc) are open source under BSD license.
Direct Hit! |
Applies To: Systems Programmer |
Goals and motivation
Let us first see what motivated Go's creators for coming up with this new
language. In the last decade, much has changed in the computing world. Libraries
have become bigger with lots of dependencies which make enterprise software
development unbearably slow, Internet and networking has becoming pervasive;
multi-core processors becoming mainstream and so on. However, systems
programming languages (like C) were not designed with these in mind.
The creators of the language found that there was a genuine need for a
systems programming language that is suitable for this new world. Go is suitable
for use in multi-core and networked world. Go, being a concurrent language has
'communication channels' feature which is much safer and different from threads
and lock-based concurrency (like Java or Pthreads).
It is faster to build applications with Go. The language is designed for fast
compilation in mind. The compiler does not need a symbol-table(!), the parser
does not need any look-ahead of tokens(!). Package dependencies are made
explicit; dependent packages require recompilation only if really needed:
features like this enables quick builds. This approach is unlike other languages
like C, C++, Java etc.
Low-level languages like 'C' are bug prone, particularly with manual memory
management and pointer manipulation. Go is garbage collected and is memory safe.
It is a strictly typed language and disallows unsafe programming (pointer
manipulation is not allowed, for example), so it is also type-safe.
Performance of Go code is within 10%-20% of the equivalent C code. Given the
fact that it supports features like reflection and garbage collection, the
ability to execute write such efficient programs is interesting (and
impressive). Go brings the ease of programming of a dynamically typed language,
like Ruby and Python. This is a surprising feature given the fact that it is a
type-safe, statically typed language. The code is also less verbose, with its
features like type-inference (unlike Java, for example).
'Hello world' Example
Here it is:
package main
func main() {
print("Hello world")
}
It prints 'Hello world' if you run it, as you would expect.
Now, let us discuss about the program. All the programs in Go should be in a
package. In Go, the execution starts with 'main.main()' function. In other
words, 'main' function should be provided in 'main' package; that is what the
first statement does. All functions are defined using 'func' keyword. The 'main'
function takes no arguments and returns nothing. We will not cover how to
process command-line arguments in this article.
Go has a set of 'built-in functions' and 'print' is one of
them. Alternatively, we could have used C-like 'Printf' function from 'fmt'
package (which provides formatted input/output functions). Note that there are
no semicolons in this program. This is a nice feature of Go: In most of the
cases, semicolons is optional. However, in few cases, we have to explicitly use
semi-colons, and we'll not cover that issue in this article.
Novel features of Go
When I started learning Go, I found that it has many novel features. One of
the best features is its interface feature (not to be confused with 'interface'
keyword in Java/C#). Any structure that implements a set of methods given by an
interface is considered to implement that interface! I saw sample code in the Go
source packages and was impressed by the simplicity and beauty of the feature.
Note that many dynamic languages support 'duck typing' which is more
sophisticated and flexible. However, Go achieves limited form of 'duck typing'
with type-safety and flexibility, in a statically typed language and that too
meant for systems programming.
Go has an elegant declaration syntax different from most
C-based languages. It supports 'goroutines' (not same as 'threads' or
'coroutines' as in other languages), which is a powerful feature for writing
concurrent programs. We can use reflection in Go (remember it is a systems
programming language!). The support for packages and the way methods are
imported/exported is simple and straight-forward (no keywords like 'public',
'export' etc are needed, for example). There are more, but I'll leave it to you
to discover them yourselves.
Installing Go
Currently, Go implementations are available for two platforms: Linux and Mac
OS X. There are two implementations for these platforms: One is a stand-alone
implementation of Go compiler and runtime, written by Ken Thompson. Another is a
GCC front-end, implemented by Ian Lance Taylor.
You can download and install Go by following the
instructions given in Go's official website: http://golang.org/doc/ install.html.
There is no official port of Go available for Windows and we can use “unofficial
ports” as of now (check unofficial Go site:
http://go-lang.cat-v.org/os-ports).
I tried installing both Windows and Linux versions, and the
installation was easy and smooth. Go certainly has some unique features that
will make it as a useful systems programming language in the age of Internet
and multi-core processors. Go won Tiobe's 'language of the year award 2009'
just after few months of its announcement!.The best feature I liked in Go is its
simplicity (yes, I am considering it a feature). In just a couple of days I have
started learning Go, I was able to write non-trivial programs and started liking
it. It was fun learning and playing with the language.
About the Author
S G Ganesh is Member Technical Staff in Siemens (Corporate Technologies &
Research), Bangalore. He has authored many books, including '60 Tips on Object
Oriented Programming' published by Tata McGraw-Hill, New Delhi..