Getting Started

Computer programming is the art, craft and science of writing programs which define how computers operate. This book will teach you how to write computer programs using a programming language designed by Google named Go.

Go is a general purpose programming language with advanced features and a clean syntax. Because of its wide availability on a variety of platforms, its robust well-documented common library, and its focus on good software engineering principles, Go is an ideal language to learn as your first programming language.

The process we use to write software using Go (and most programming languages) is fairly straightforward:

This process is iterative (meaning its done many times) and the steps usually overlap. But before we write our first program in Go there are a few prerequisite concepts we need to understand.

Files and Folders

A file is a collection of data stored as a unit with a name. Modern operating systems (like Windows or Mac OSX) contain millions of files which store a large variety of different types of information – everything from text documents to executable programs to multimedia files.

All files are stored in the same way on a computer: they all have a name, a definite size (measured in bytes) and an associated type. Typically the file's type is signified by the file's extension – the part of the file name that comes after the last .. For example a file with the name hello.txt has the extension txt which is used to represent textual data.

Folders (also called directories) are used to group files together. They can also contain other folders. On Windows file and folder paths (locations) are represented with the \ (backslash) character, for example: C:\Users\john\example.txt. example.txt is the file name, it is contained in the folder john, which is itself contained in the folder Users which is stored on drive C (which represents the primary physical hard drive in Windows). On OSX (and most other operating systems) file and folder paths are represented with the / (forward slash) character, for example: /Users/john/example.txt. Like on Windows example.txt is the file name, it is contained in the folder john, which is in the folder Users. Unlike Windows, OSX does not specify a drive letter where the file is stored.

Windows

On Windows files and folders can be browsed using Windows Explorer (accessible by double-clicking “My Computer” or typing win+e):

OSX

On OSX files and folders can be browsed using Finder (accessible by clicking the Finder icon – the face icon in the lower left bar):

The Terminal

Most of the interactions we have with computers today are through sophisticated graphical user interfaces (GUIs). We use keyboards, mice and touchscreens to interact with visual buttons or other types of controls that are displayed on a screen.

It wasn't always this way. Before the GUI we had the terminal – a simpler textual interface to the computer where rather than manipulating buttons on a screen we issued commands and received replies. We had a conversation with the computer.

And although it might appear that most of the computing world has left behind the terminal as a relic of the past, the truth is that the terminal is still the fundamental user interface used by most programming languages on most computers. The Go programming language is no different, and so before we write a program in Go we need to have a rudimentary understanding of how a terminal works.

Windows

In Windows the terminal (also known as the command line) can be brought up by typing the windows key + r (hold down the windows key then press r), typing cmd.exe and hitting enter. You should see a black window appear that looks like this:


By default the command line starts in your home directory. (In my case this is C:\Users\caleb) You issue commands by typing them in and hitting enter. Try entering the command dir, which lists the contents of a directory. You should see something like this:

C:\Users\caleb>dir
Volume in drive C has no label.
Volume Serial Number is B2F5-F125

Followed by a list of the files and folders contained in your home directory. You can change directories by using the command cd. For example you probably have a folder called Desktop. You can see its contents by entering cd Desktop and then entering dir. To go back to your home directory you can use the special directory name .. (two periods next to each other): cd ... A single period represents the current folder (known as the working folder), so cd . doesn't do anything. There are a lot more commands you can use, but this should be enough to get you started.

OSX

In OSX the terminal can be reached by going to Finder → Applications → Utilities → Terminal. You should see a window like this:


By default the terminal starts in your home directory. (In my case this is /Users/caleb) You issue commands by typing them in and hitting enter. Try entering the command ls, which lists the contents of a directory. You should see something like this:

caleb-min:~ caleb$ ls
Desktop      Downloads      Movies     Pictures
Documents    Library        Music      Public

These are the files and folders contained in your home directory (in this case there are no files). You can change directories using the cd command. For example you probably have a folder called Desktop. You can see its contents by entering cd Desktop and then entering ls. To go back to your home directory you can use the special directory name .. (two periods next to each other): cd ... A single period represents the current folder (known as the working folder), so cd . doesn't do anything. There are a lot more commands you can use, but this should be enough to get you started.

Text Editors

The primary tool programmers use to write software is a text editor. Text editors are similar to word processing programs (Microsoft Word, Open Office, …) but unlike such programs they don't do any formatting, (No bold, italic, …) instead they operate only on plain text. Both OSX and Windows come with text editors but they are highly limited and I recommend installing a better one.

To make the installation of this software easier an installer is available at the book's website: http://www.golang-book.com/. This installer will install the Go tool suite, setup environmental variables and install a text editor.

Windows

For windows the installer will install the Scite text editor. You can open it by going to Start → All Programs → Go → Scite. You should see something like this:


The text editor contains a large white text area where text can be entered. To the left of this text area you can see the line numbers. At the bottom of the window is a status bar which displays information about the file and your current location in it (right now it says that we are on line 1, column 1, text is being inserted normally, and we are using windows-style newlines).

You can open files by going to File → Open and browsing to your desired file. Files can be saved by going to File → Save or File → Save As.

As you work in a text editor it is useful to learn keyboard shortcuts. The menus list the shortcuts to their right. Here are a few of the most common:

OSX

For OSX the installer installs the Text Wrangler text editor:

Like Scite on Windows Text Wrangler contains a large white area where text is entered. Files can be opened by going to File → Open. Files can be saved by going to File → Save or File → Save As. Here are some useful keyboard shortcuts: (Command is the ⌘ key)

Go Tools

Go is a compiled programming language, which means source code (the code you write) is translated into a language that your computer can understand. Therefore before we can write a Go program, we need the Go compiler.

The installer will setup Go for you automatically. We will be using version 1 of the language. (More information can be found at http://www.golang.org)

Let's make sure everything is working. Open up a terminal and type the following:

go version

You should see the following:

go version go1.0.2

Your version number may be slightly different. If you get an error about the command not being recognized try restarting your computer.

The Go tool suite is made up of several different commands and sub-commands. A list of those commands is available by typing:

go help

We will see how they are used in subsequent chapters.

Index Next →