Programming with Go

Modern Classic

Tools

If you want to start programming in Go, you typically only need to install the compiler; the more recent Linux distributions already have it in their package management systems. To install on Ubuntu 12.04, type:

apt-get install golang

Other operating systems, including Mac and Windows, have binary distributions of the Go package.

After the installation, your hard drive will contain (depending on the processor architecture) the commands 6a, 6c, 6g, and 6l – or their counterparts with the digits 5 or 8. The 6 tools are for AMD64 processors, the 5 series are for 368 processors, and the 8 series are for ARM processors. The strange names are inherited from Plan9, which uses these digits to identify the processor architectures.

Conveniently, you don't have to worry about these details in practical terms and can instead just use the front end go. It is also worth mentioning that the compiler-generated object files with the extension .6 contain references to all modules used, which the linker then follows. This avoids the problem of having to find the appropriate libraries, in addition to the header files, and specifying them during the linker process, as is the case with C and C++. If a Go program builds, the compiler can it also link it. This means that well-known linker problems are a thing of the past. go also handles the task of makefiles.

If you installed Go correctly, you should be able to run the go command at the console:

$ go version
go version go1

If the Go installation is not automatically added to your PATH for executable files, you should point the environment variable GOROOT to the parent directory and then add $GOROOT/bin to the PATH.

Because Go programs build really fast, they are well-suited to calling scripts, with certain limitations – that is, as a kind of do-it-yourself, just-in-time compilation. The Gorun tool supports this process [5].

Advanced

Despite removing many features found in other languages, Go adds modern features desired by programmers, such as closures, which in Go are anonymous functions that save their environment.

Go achieves its stated objective of simple programming for multicore systems (Go was developed in the pre-cloud era when multicore processors were the holy grail) with an abstraction called Goroutines, which follows the communicating sequential processes (CSP) model to facilitate error-prone programming with threads [6].

For further reading, check out the online document "Effective Go," which shows some "idiomatic" Go solutions [7]. A variety of books on Go have been published, and more help is available on the golang-nuts mailing list (the golang-dev list is intended for compiler developers). Finally, a tip for your web search for Go resources: Instead of typing "go" as the search term, always use "golang." Locally, you can read the entire documentation in the browser if you launch the documentation server by typing

godoc -http=:8000

Go comes with a variety of libraries (Figure 2); you can find the complete list at a number of sites [8]-[10]. Larger projects implemented in Go include the Falcore web server [11] and the StatHat [12] web software.

Figure 2: A few of the Go packages.

Conclusions

Go is mature and, thanks to forward compatibility promised by the developers, is suitable for professional software projects. Also, the short build times are useful for large projects. Reduction in the language scope makes it relatively easy to learn and keeps third-party code easy to read, which will ultimately benefit the serviceability of any piece of software.

The price for this reduction to the essential is the absence of such features as class-based object orientation, which many programmers are accustomed to having. Go offers various features that allow object-oriented development but requires some accommodation of new concepts. Go is expected to be implemented in an increasing number of projects, especially in the area of system programming, but support on Google's App Engine could also make it interesting for use in web programming.

Buy this article as PDF

Download Article PDF now with Express Checkout
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

Related content

comments powered by Disqus