1.2. Editing, Compiling, and Running
Go programs are written as plain text Unicode using the UTF-8 encoding.1 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 we edit, compile, and run a Go program we’ll start with the classic “Hello World” program—although we’ll make it a tiny bit more sophisticated than usual. First we will discuss compiling and running, then in the next section we will go through the source code—in file hello/hello.go—in detail, since it incorporates some basic Go ideas and features.
All of the book’s examples are available from http://www.qtrac.eu/gobook.html and unpack to directory goeg. So file hello.go’s full path (assuming the examples were unpacked in the home directory—although anywhere will do) is $HOME/goeg/src/hello/hello.go. When referring to files the book always assumes the first three components of the path, which is why in this case the path is given only as hello/hello.go. (Windows users must, of course, read “/”s as “\”s and use the directory they unpacked the examples into, such as C:\goeg or %HOMEPATH%\goeg.)
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 (xterm, gnome-terminal, konsole, Terminal.app, or similar):
Or on Windows in an MS-DOS Prompt or Command Prompt window:
If you get a “command not found” or “‘go’ is not recognized...” error message then it means that Go isn’t in the PATH. The easiest way to solve this on Unix-like systems (including Mac OS X) is to set the environment variables in .bashrc (or the equivalent file for other shells). For example, the author’s .bashrc file contains these lines:
Naturally, you must adjust the values to match your own system. (And, of course, this is only necessary if the go version command fails.)
On Windows, one solution is to create a batch file that sets up the environment for Go, and to execute this every time you start a console for Go programming. However, it is much more convenient to set the environment variables once and for all through the Control Panel. To do this, click Start (the Windows logo), then Control Panel, then System and Security, then System, then Advanced system settings, and in the System Properties dialog click the Environment Variables button, then the New... button, and add a variable with the name GOROOT and a suitable value, such as C:\Go. In the same dialog, edit the PATH environment variable by adding the text ;C:\Go\bin at the end—the leading semicolon is vital! In both cases replace the C:\Go path component with the actual path where Go is installed if it isn’t C:\Go. (Again, this is only necessary if the go version command failed.)
From now on we will assume that Go is installed and the Go bin directory containing all the Go tools is in the PATH. (It may be necessary—once only—to open a new console window for the new settings to take effect.)
Two steps are required to build Go programs: compiling and linking.2 Both of these steps are handled by the go tool which can not only build local programs and packages, but can also fetch, build, and install third-party programs and packages.
For the go tool to be able to build local programs and packages, there are three requirements. First, the Go bin directory ($GOROOT/bin or %GOROOT%\bin) must be in the path. Second, there must be a directory tree that has an src directory and under which the source code for the local programs and packages resides. For example, the book’s examples unpack to goeg/src/hello, goeg/src/bigdigits, and so on. Third, the directory above the src directory must be in the GOPATH environment variable. For example, to build the book’s hello example using the go tool, we must do this:
We can do almost exactly the same on Windows:
In both cases we assume that the PATH includes $GOROOT/bin or %GOROOT%\bin. Once the go tool has built the program we can run it. By default the executable is given the same name as the directory it is in (e.g., helloon Unix-like systems and hello.exe on Windows). Once built, we can run the program in the usual way.
On Windows it is very similar:
We have shown what must be typed in bold and the console’s text in roman. We have also assumed a $ prompt, but it doesn’t matter what it is (e.g., C:\>).
Note that we do not need to compile—or even explicitly link—any other packages (even though as we will see, hello.go uses three standard library packages). This is another reason why Go programs build so quickly.
If we have several Go programs, it would be convenient if all their executables could be in a single directory that we could add to our PATH. Fortunately, the go tool supports this as follows:
Again, we can do the same on Windows:
The go install command does the same as go build only it puts the executable in a standard location ($GOPATH/bin or %GOPATH%\bin). This means that by adding a single path ($GOPATH/bin or %GOPATH%\bin) to our PATH, all the Go programs that we install will conveniently be in the PATH.
In addition to the book’s examples, we are likely to want to develop our own Go programs and packages in our own directory. This can easily be accommodated by setting the GOPATH environment variable to two (or more) colon-separated paths (semicolon-separated on Windows); for example, export GOPATH=$HOME/app/go:$HOME/goeg or SET GOPATH=C:\app\go;C:\goeg. 3 In this case we must put all our program and package’s source code in $HOME/app/go/src or C:\app\go\src. So, if we develop a program called myapp, its .go source files would go in $HOME/app/go/src/myapp or C:\app\go\src\myapp. And if we use go install to build a program in a GOPATH directory where the GOPATH has two or more directories, the executable will be put in the corresponding directory’s bin directory.
Naturally, it would be tedious to export or set the GOPATH every time we wanted to build a Go program, so it is best to set this environment variable permanently. This can be done by setting GOPATH in the .bashrc file (or similar) on Unix-like systems (see the book’s example’s gopath.sh file). On Windows it can be done either by writing a batch file (see the book’s example’s gopath.bat file), or by adding it to the system’s environment variables: Click Start (the Windows logo), then Control Panel, then System and Security, then System, then Advanced system settings, and in the System Properties dialog click the Environment Variables button, then the New... button, and add a variable with the name GOPATH and a suitable value, such as C:\goeg or C:\app\go;C:\goeg.
Although Go uses the go tool as its standard build tool, it is perfectly possible to use make or some of the modern build tools, or to use alternative Go-specific build tools, or add-ons for popular IDEs (Integrated Development Environments) such as Eclipse and Visual Studio.