Writing Go code is a fun and enjoyable experience where compile-time errors—rather than being a pain—actually guide you to write robust, high-quality code. However, every now and again, you will encounter environmental issues that start to get in the way and break your flow. While you can usually resolve these issues after some searching and a little tweaking, setting up your development environment correctly goes a long way in reducing problems, allowing you to focus on building useful applications.
In this chapter, we are going to install Go from scratch on a new machine and discuss some of the environmental options we have and the impact they might have in the future. We will also consider how collaboration might influence some of our decisions, as well as what impact open sourcing our packages might have.
Specifically, we are going to:
GOPATH
environment variable is for, and discuss a sensible approach to its use.go
files, and how we can integrate the Go tools as part of our daily developmentGo is an open source project written originally in C, which means we can compile our own version from the code easily; this remains the best option for installing Go for a variety of reasons. It allows us to navigate through the source if we need to look something up later, either in the standard library Go code, or in the C code of the tools. It also allows us to easily update to newer versions of Go or experiment with release candidates as they come out, just by pulling a different tag or branch from the code repository and building again. Of course, we can also easily roll back to earlier versions if we need to, and even fix bugs and generate pull requests to send to the Go core team for them to consider contributions to the project.
A continually updated resource for installing Go from its source on a variety of platforms can be found online at http://golang.org/doc/install/source or by searching for Install Golang from source
. This chapter will cover the same things, but if you encounter problems, the Internet is going to be your best friend in helping resolve issues.
Since the Go tool chain is written in C, we will actually be compiling C code when we build our Go installation. This may seem a little counter-intuitive; a programming language was written using a different programming language, but of course, Go didn't exist when the Go core team started writing Go, but C did. It is more accurate to say that the tools used to build and link Go programs are written in C. Either way, for now, we need to be able to compile the C source code.
To determine whether you need to install the C tools or not, open a terminal and try to use the gcc
command:
gcc -v
If you receive a command not found
error or similar, you will likely have to install the C tools. If, however, you see the output from gcc
giving you version information (that's what the -v
flag was for), you can likely skip this section.
Installing C tools differs for various platforms and could change over time, so this section should be treated only as a rough guide to help you get the tools you need.
The tools on a Mac running OS X are shipped with Xcode, which is available in App Store for free. Once you install Xcode, you open Preferences and navigate to the Downloads section. From there, you find the command-line tools that include the C tools you will need to build Go.
On Ubuntu and Debian systems, you can use apt-get
to install the tools:
sudo apt-get install gcc libc6-dev
For RedHat and Centos 6 systems, you can use yum
to install the tools:
sudo yum install gcc glibc-devel
For Windows, the MinGW project offers a Windows installer that will install the tools for you. Navigate to http://www.mingw.org/ and follow the instructions there to get started.
Once you have successfully installed the tools and ensured the appropriate binaries are included in your PATH
environment variable, you should be able to see some sensible output when running gcc -v
:
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) Target: x86_64-apple-darwin13.2.0 Thread model: posix
The preceding snippet is the output on an Apple Mac computer, and the most important thing to look for is the lack of the command not found
error.
The Go source code is hosted at Google Code in a Mercurial repository, so we will use the hg
command to clone it in preparation for building.
If you do not have the hg
command, you can get Mercurial from the download page at http://mercurial.selenic.com/downloads.
In a terminal, to install Go, navigate to a suitable location such as /opt
on Unix systems, or C:
on Windows.
Get the latest release of Go by typing the following command:
hg clone -u release https://code.google.com/p/go
After a while, the latest Go source code will download into a new go
folder.
Navigate to the go/src
folder that was just created and run the all
script, which will build an instance of Go from the source code. On Unix systems this is all.bash
, on Windows it's all.bat
.
Once all the build steps are complete, you should notice that all the tests have successfully passed.
3.129.42.134