Channels ▼
RSS

Mobile

Getting Going with Go


This tutorial series will run for five consecutive weeks. In this installment, Go expert Mark Summerfield explains how to set up Go, and then he provides two examples of Go programs that are explained in depth. The programs provide a partial overview of Go's key features and some of its key packages. The following weeks' installments will show the remaining key features and dig into many aspects that make Go a uniquely interesting language, especially for C, C++, and Java programmers.

As explained in this week's editorial, Go has many unique features and might be described as C for the 21st century. Given that one of the language's designers is Ken Thompson, the languages do indeed share a common ancestor. —Ed.


Getting Going

Go programs are compiled rather than interpreted. Compilation is very fast—dramatically faster than some other languages—most notably C and C++.

The standard Go compiler is called gc and its toolchain includes programs such as 5g, 6g, and 8g for compiling, 5l, 6l, and 8l for linking, and godoc for viewing the Go documentation. (These are 5g.exe, 6l.exe, and so forth, on Windows.) The strange names follow the Plan 9 operating system's compiler naming conventions where the digit identifies the processor architecture (“5” for ARM, “6” for AMD64—including Intel 64-bit processors—and “8” for Intel 386.) Fortunately, we don't need to concern ourselves with these tools, since Go provides the high-level go build tool that handles the compiling and linking for us.

All the Go code in this article has been tested using gc on Linux, Mac OS X, and Windows using Go 1. The Go developers intend to make all subsequent Go 1.x versions backward compatible with Go 1, so the text and examples here should be valid for the entire 1.x series.

To download and install Go, visit golang.org/doc/install.html, which provides instructions and download links. Go 1 is available in source and binary form for FreeBSD 7+, Linux 2.6+, Mac OS X (Snow Leopard and Lion), and Windows 2000+, in all cases for Intel 32-bit and AMD 64-bit processor architectures. There is also support for Linux on ARM processors. Prebuilt go packages are available for the Ubuntu Linux distribution, and may be available for other Linux distros by the time you read this.

Programs built with gc use a particular calling convention. This means that programs compiled with gc can be linked only to external libraries that use the same calling convention—unless a suitable tool is used to bridge the difference. Go comes with support for using external C code from Go programs in the form of the cgo tool, and at least on Linux and BSD systems, both C and C++ code can be used in Go programs using the SWIG tool.

In addition to gc there is also the gccgo compiler. It is a Go-specific front end to gcc available for gcc from version 4.6. Like gc, gccgo may be available prebuilt for some Linux distributions. Instructions for building and installing gccgo gccgo are given on the main Go website.

The Go Documentation

Go's official website hosts the most up-to-date Go documentation. The “Packages” link provides access to the documentation on all the Go standard library's packages—and to their source code, which can be very helpful when the documentation itself is sparse. The “Commands” link leads to the documentation for the programs distributed with Go (such as the compilers, build tools, etc.). The “Specification” link leads to an accessible, informal, and quite thorough Go language specification. And the “Effective Go” link leads to a document that explains many best practices.

The website also features a sandbox in which small (somewhat limited) Go programs can be written, compiled, and run, all online. This is useful for beginners for checking odd bits of syntax. The Go website's search box searches only the Go documentation; to search for Go resources generally, visit http://go-lang.cat-v.org/go-search.

The Go documentation can also be viewed locally, for example, in a web browser. To do this, run Go's godoc tool with a command-line argument that tells it to operate as a web server. Here's how to do this in a Unix or Windows console, presuming godoc is in your PATH:

$ godoc -http=:8000 

The port number used here is arbitrary—simply use a different one if it conflicts with an existing server.

To view the served documentation, open a web browser and give it a location of http://localhost:8000. This will present a page that looks very similar to the golang.org web site's front page. The “Packages” link will show the documentation for Go's standard library, plus any third-party packages that have been installed under GOROOT. If GOPATH is defined (e.g., for local programs and packages), a link will appear beside the “Packages” link through which the relevant documentation can be accessed. (The GOROOT and GOPATH environment variables are discussed later in this article.)

Editing, Compiling, and Running

Go programs are written as plain text Unicode using the UTF-8 encoding. Most modern text editors can handle this automatically, and some of the most popular may even have support for Go color syntax highlighting and automatic indentation. If your editor doesn't have Go support, try entering the editor's name in the Go search engine to see if there are suitable add-ons. For editing convenience, all of Go's keywords and operators use ASCII characters; however, Go identifiers can start with any Unicode letter followed by any Unicode letters or digits, so Go programmers can freely use their native language.

To get a feel for how to edit, compile, and run a Go program I'll start with the classic “Hello World” program—although we'll make it a tiny bit more sophisticated than usual.

If you have installed Go from a binary package or built it from source and installed it as root or Administrator, you should have at least one environment variable, GOROOT, which contains the PATH to the Go installation, and your PATH should now include $GOROOT/bin or %GOROOT%\bin. To check that Go is installed correctly, enter the following in a console:

$ go version

If you get a “command not found” or “‘go' is not recognized...” error message then it means that Go is not in the PATH.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video