Book Image

Go Programming Blueprints

By : Mat Ryer
Book Image

Go Programming Blueprints

By: Mat Ryer

Overview of this book

Table of Contents (17 chapters)
Go Programming Blueprints
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Installing Go


Go 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.

Note

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.

Installing the C tools

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.

Note

At the first ever Gophercon in Denver, Colorado in 2014, Rob Pike and the team expressed that one of their goals would be to replace the C tool chain with programs written in Go— so that the entire stack becomes Go. At the time of writing, this hasn't happened yet, so we will need the C tools.

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.

Downloading and building Go from the source

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.

Note

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.