Go Setup - Windows

Overview

This course will show you how to set up a Windows machine for modern software development with Go.

Core Steps:

  • Install GCC
  • Install and configure Git
  • Set up environment variables such as GOPATH and adding BIN to your existing PATH
  • Install Visual Code for editing your source code

Optional Steps:

  • Install PostgreSQL
  • Install SQLite
  • Install Node/NPM
  • Install Docker

Setting Up Windows

In order to use Go on Windows there are a few programs, tools, and applications we need to configure and install.

This module will walk you through that process on a Windows 10 machine. If you are using an older version of Windows it is recommended that you either upgrade to a newer version, or you will need to Google for the correct instructions for each step.

Install A GCC

A GCC (GNU Compiler Collection) is not absolutely required for Go, but it is required for other tools, so it is best to install it before installing anything else.

  1. Navigate to http://tdm-gcc.tdragon.net/.
  2. Download the TDM64 bundle.
  3. Once downloaded Windows should prompt you to run the installer. Click Run.
  4. Select Create in the installer prompt.
  5. Select MinGW-w64/TDM64 bundle.
  6. Click through the "next" prompts until you get to the "Choose Components" page.
  7. On the "Choose Components" page, select Install.

Verify The GCC Installation

  1. Click the "Start" menu
  2. Type in cmd and press "enter".
  3. In the terminal type gcc.
  4. You should receive the following message: gcc: fatal error: no input files

If you do not get the above message your installation is NOT complete. Recheck the steps and try again.

Installing Git

Git is a popular SCM (source code management) tool that is used heavily by Go projects.

  1. Navigate to http://git-scm.com/download/win
  2. Download the 64-bit git for windows setup installer.
  3. Once downloaded Windows should prompt you to run the installer. Click Run.
  4. Click through all the "next" prompts.

Verifying The Git Installation

  1. Click the "Start" menu
  2. Type in cmd and press "enter".
  3. In the terminal type git --version and press "enter".
  4. You should receive the following message: git version 2.x.x.windows

If you do not get the above message your installation is NOT complete. Recheck the steps and try again.

Configuring Git

  1. Click the "Start" menu
  2. Type in cmd and press "enter".
  3. In the terminal type git config --global user.email "<your email address>" and press "enter".
  4. In the terminal type git config --global user.name "<your name>" and press "enter".
  5. Type git config -l to verify your information.

Configure Git Keys/Certificates (Optional)

Git can be configured to use SSH keys. This makes it so you don't have to constantly provide your password at the command line to get or send commits to the repositories. You can find detailed instructions on how to do this here:

Generating a new SSH key and adding it to the ssh-agent

For additional security, you can also sign your commits and they will get the verified icon on github. Follows these instructions to set up signed commits using GPG:

Signing commits using GPG

Installing Go

  1. Navigate to golang.org/dl
  2. Click on the Windows installer to download it.
  3. If you get a warning that golang.org is "unsafe", click on "More information", then click "Disregard and continue".
  4. Once downloaded Windows should prompt you to run the installer. Click Run.
  5. Click through all the "next" prompts.

Verifying The Go Installation

  1. Click the "Start" menu
  2. Type in cmd and press "enter".
  3. In the terminal type go version and press "enter".
  4. You should receive the following message: go version go1.x.x windows/amd64.

Setting Up Environment Variables

Go requires a couple of environment variables to be configured for development.

These variables are:

  • GOPATH - This is where all of your Go source code will be. This is NOT the path of the Go tooling we installed previously.
  • PATH - This tells your system where to find executable files. We need to update this to find the Go executable we will be creating.

Set The %GOPATH% Variable

  1. Click the "Start" menu
  2. Type in environment variables and select "Edit environment variables for your account".
  3. In the "User variables for <name>"
  4. Click "New"
  5. Set the "Variable name" to GOPATH
  6. Set the "Variable value" to %USERPROFILE%\go
  7. Click "OK"

Verifying The %GOPATH% Environment Variable

  1. Click the "Start" menu
  2. Type in cmd and press "enter".
  3. In the terminal type echo %GOPATH% and press "enter".
  4. You should receive the following message: C:\Users\<name>\go.

If you do not get the above message your installation is NOT complete. Recheck the steps and try again.

Add Go Bin Directory To %PATH%

For convenience, add the workspace's bin subdirectory to your PATH:

  1. Click the "Start" menu
  2. Type in environment variables and select "Edit environment variables for your account".
  3. In the "User variables for <name>"
  4. Select the Path variable
  5. Click "Edit"
  6. Click "New"
  7. Type %GOPATH%\bin
  8. Click "OK"
  9. Click "OK" again to dismiss the environment variables window

Verifying The %PATH% Environment Variable

  1. Click the "Start" menu
  2. Type in cmd and press "enter".
  3. In the terminal type echo %PATH% and press "enter".
  4. You should see the following in the printed out text: C:\Users\<name>\go\bin.

If you do not get the above message your installation is NOT complete. Recheck the steps and try again.

$GOPATH Is Not $GOROOT

While you don't need to specifically setup the $GOROOT variable anymore, you might still see it referenced in older materials. This is the path of your GO installation, not your source code.

The $GOROOT is usually something like /usr/local/go. Your $GOPATH is usually something like $HOME/go.

The Go binary distributions assume they will be installed in c:\Go , but it is possible to install the Go tools to a different location. In this case you must set the GOROOT environment variable to point to the directory in which it was installed.

Go Workspaces

Under the $GOPATH there are three folders:

.
├── bin
├── pkg
└── src

bin

This is where compiled Go programs will be installed.

pkg

Compiled package objects. You can safely ignore this directory.

src

This is where all of your source code for Go projects has to live.

Common Layout

It is common to layout out your Go project in the following directory structure:

$GOPATH/src/github.com/username/project

This will make projects available with the go get tool. It will also help readability later.

Exercise: System Check

package main

import (
	"log"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		log.Fatal("Your GOPATH has not been set!")
	}

	path := os.Getenv("PATH")
	gobin := filepath.Join(gopath, "bin")
	if !strings.Contains(path, gobin) {
		log.Fatalf("Your PATH does not contain %s", gobin)
	}

	log.Println("Success!")
}

file: ../mac-linux/src/env_check.go

package main

import (
	"log"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		log.Fatal("Your GOPATH has not been set!")
	}

	path := os.Getenv("PATH")
	gobin := filepath.Join(gopath, "bin")
	if !strings.Contains(path, gobin) {
		log.Fatalf("Your PATH does not contain %s", gobin)
	}

	log.Println("Success!")
}

file: ../mac-linux/src/env_check.go

$ go run main.go

If you see anything other than Success, your GOPATH is not correctly set up.

Installing Visual Studio Code (optional)

Visual Studio Code, or VS Code, is a great all purpose editor, and it has great support for Go.

  1. Navigate to http://code.visualstudio.com/download.
  2. Select the Windows installer.
  3. Once downloaded Windows should prompt you to run the installer. Click Run.
  4. Click through the "next" prompts until the "Select Additional Tasks" prompt.
  5. On the "Select Additional Tasks" prompt select all options.
  6. Click "OK" to finish the installation.

Configuring Visual Studio Code (optional)

  1. Open Visual Studio Code
  2. In the menu navigate to View and then Command Palette.
  3. In the command palette type the following Extensions: Install Extensions. Click on the matching result.
  4. Install the Go plugin by lukehoban.
  5. Close and re-open Visual Studio Code.
  6. In the command palette type the following Go install and click on Go: Install/Update Tools

PostgreSQL (optional)

PostgreSQL is a great, free, full-featured relational database that is very popular.

  1. Navigate to http://postgresql.org/download/windows.
  2. Click "Download the graphical installer from BigSQL".
  3. Select the PostgreSQL 9.x.x windows installer
  4. Once downloaded Windows should prompt you to run the installer. Click Run.
  5. Click through the "next" prompts until the "Select Components" prompt.
  6. On the "Select Components" page check all the options.
  7. Set the password to postgres and then click "Next".
  8. On the "pgDevOps" screen fill in your information.
  9. Click through all the "next" prompts.

SQLite3 (Optional)

SQLite is a file based, relational database.

  1. Navigate to http://www.sqlite.org/download.html.
  2. Go to Precomiled Binaries for Windows.
  3. Download the 64-DLL zip file
  4. Open the zip file by double clicking on it.
  5. Copy sqlite3.dll to C:\Windows\System32.

Node/NPM (Optional)

NPM is considered the tool for managing modern front-end web development. To install NPM we must also install Node.

  1. Navigate to http://nodejs.org/en/download
  2. Click on the Windows installer.
  3. Once downloaded Windows should prompt you to run the installer. Click Run.
  4. Click through all the "next" prompts.

Verifying Node/NPM Installation (optional)

  1. Click the "Start" menu
  2. Type in cmd and press "enter".
  3. In the terminal type node --version.
  4. You should receive the following message: v8.x.x

If you do not get the above message your installation is NOT complete. Recheck the steps and try again.

Docker (Optional)

Docker is a popular tool for running and managing containers for development as well as deployment.

NOTE: Docker for Windows is not supported on Windows 10 Home, only on Windows 10 Pro.

  1. Navigate to http://docker.com/docker-windows
  2. Click "Download from Docker Store".
  3. Click "Get Docker"
  4. Once downloaded Windows should prompt you to run the installer. Click Run.
  5. Click through all the "next" prompts.

VPN Issues

Many VPN's block access to sites like gihtub.com, specifically when you are at the command line and using git commands.

If you are experiencing issues with setting up, or retrieving code repositories with git or the go get command, check your VPN settings or connect to a public network to issue the commands.