While the Go language has a web-based playground, for serious work you're better off with a locally installed development environment. This tutorial will show you how to set up a local "Go dev. environment," all the way through to writing and running your first (really basic) program.

NOTE: While some of the instructions here are Windows specific, all software is available for Linux, MacOS, etc., too.

Step One: Install Go

The first step is to download and install the Go compiler and tools. These can be downloaded from:

http://golang.org

Installation isn't quite as straightforward as I'd hope. Follow the instructions here (https://golang.org/doc/install), and then read on.

Set the GOPATH

The GOPATH is where the Go compiler/tools expect your source-code to be. It's also where it'll install other packages, and put the compiled binaries. So it's important that you set it. Failure to do so will cause trouble later. At the time of writing this tutorial (May 2016), the installer does NOT do it for you.

First, create a directory that you will use as your workspace. I called mine "GoWork," and placed it in my home directory ("C:\Users\Hans\GoWork"). Its up to you where you put it.

Next, create the GOPATH environment variable. On Windows, right-click on the start button, and select "System" (Windows 8 users, go to the control panel to find "System"). Next, click on "Advanced System Settings," and then select "Environment Variables."

Since your GOPATH belongs to you personally, you want to add a new "user" variable called GOPATH that points to your Go workspace (in my case its "C:\Users\Hans\GoWork"). Once you're done, it should look something like the following.

Set GOPATH Env Var

Windows Users Must Reboot Before Continuing

On Windows, there's a good chance that GOROOT won't be active yet, so restart your machine. I didn't do this, and ran into trouble when installing the code editor. Seriously, save yourself some trouble, and reboot now!

Step Two: Install Git

We're going to need various dependencies from Git repositories. Go can automatically fetch such dependencies, but only if Git is installed. So, download and install Git from https://git-scm.com/downloads.

Step Three: Install the Code Editor

Writing code is a lot more fun with an Integrated Development Environment (IDE), or at least a code editor that does syntax highlighting. There are various options out there that support Go. I've found that GitHub's Atom works pretty well, provided that you install a few plugins too. Download and install Atom from: https://atom.io/

Install the Go-Plus & Script Plugins

The go-plus plugin provides us with Go syntax highlighting (amongst other things), while the script plugin enables us to compile and run Go programs directly from within Atom. To install them, select File => settings => install from the menus. Enter "go-plus" in the search box, and push enter. Now, click on the go-plus plugin's install button.

Install go-plus

Now enter "script" in the search box, and install the script plugin.

NOTE: Make sure you install the plugin called "script," and not some variation. It's the one shown below.

Install Atom's cript plugin

Choose Your Theme (Optional)

This is entirely a matter of personal preference. Atom's default theme is a dark one, with light text on a dark background. I personally perfer dark text on a light background; it's easier on the eyes. You'll find a "Themes" section in the settings.

Step Four: Set Up Your Workspace

Nearly there. We just need to set the code editor to use our workspace. From the menus, select File => Open Folder, and choose the "src" subdirectory of your GOPATH. In my case that's "C:\Users\Hans\GoWork\src."

Step Five: Write and Run Your First Go Program

Okay, we're finally ready to write our first Go program. In Atom, right-click on the "src" directory (in the left column), and select "New Folder." Create a folder called "scratchpad." Next, create a new file in scratchpad called "hello.go" (right-click on scratchpad and select "New File").

NOTE: I'm departing from standard Go convention here. Normally, the source-code repository's URL is used as the base directory for files (e.g., Go's Lint is stored in "github.com/golang/lint/"). However, it would be overkill to set up a repository for something as simple as hello.go. I use "scratchpad" as a place for small test programs.

Enter the following code into hello.go, and save it:

package main

import "fmt"

func main() {
	fmt.Printf("Hello, world.\n")
}

Auto-Formatting

One nice feature in Go, is gofmt. This formats your code according to the language's conventions. You can access this in Go via the Packages menu. Select Packages => Go => Format With gofmt.

Running Hello.go

It's finally time to run our first program. With hello.go still open in the editor, select Packages => Script => Run Script.

This will compile and run hello.go. You should see a bar pop up at the bottom of the window with the title "Go - hello.go." Once compilation is complete, it'll run, and the console output will be printed at the bottom of the page (see below).

Atom Hello world complete

Ready for More

Now that you've got your "Go dev. environment" set up, I'm sure that you're itching to move on to writing more interesting software. I'll cover more later. In the meantime, if you haven't done so already then I recommend going through the Go tutorial here.

Feel free to contact me if you have any questions. Comments are welcome too (there's a comments box below).