-
Overview
-
Download Go Installer
-
Verifying The Go Installation
-
The
$GOPATH
-
Go Workspaces
-
Common Layout
-
Creating Your Workspace
-
Set The
%GOPATH%
Variable -
Verifying The
%GOPATH%
Environment Variable -
Add Go Bin Directory To
%PATH%
-
Verifying The
%PATH%
Environment Variable -
$GOPATH
Is Not$GOROOT
-
Exercise: System Check
-
Download Git Installer
-
Verifying The Git Installation
-
Configuring Git
-
Configure Git Keys/Certificates (Optional)
-
Download Visual Studio Code
-
Configuring Visual Studio Code (optional)
-
Install A GCC (optional)
-
Verify The GCC Installation
-
Installing PostgreSQL (optional)
-
SQLite3 (Optional)
-
Node/NPM (Optional)
-
Verifying Node/NPM Installation (optional)
-
Docker (Optional)
-
VPN Issues
Download Source
Go Setup - Windows
Overview
This course will show you how to set up a Windows machine for modern software development with Go.
Core Steps:
- Set up environment variables such as
GOPATH
and addBIN
to your existingPATH
- Install and configure Git
- Install Visual Code for editing your source code
Optional Steps:
- Install GCC
- Install PostgreSQL
- Install SQLite
- Install Node/NPM
- Install Docker
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.
Installing Go
Download Go Installer
- Navigate to golang.org/dl
- Click on the Windows installer to download it.
- If you get a warning that golang.org is "unsafe", click on "More information", then click "Disregard and continue".
- Once downloaded Windows should prompt you to run the installer. Click
Run
. - Click through all the "next" prompts.
Verifying The Go Installation
- Click the "Start" menu
- Type in
cmd
and press "enter". - In the terminal type
go version
and press "enter". - You should receive the following message:
go version go1.x.x windows/amd64
.
-
The
x
's in the version number is a placeholder for the most recent version number.
The $GOPATH
The $GOPATH
is where all Go files must live.
Go 1.8
In Go 1.8, the $GOPATH
defaults to $HOME/go
if not set explicitly. All earlier versions of Go require this environment variable to be set.
ALWAYS EXPLICITLY SET THE GOPATH
Many tools, especially third party tooling, still depend on an explicit $GOPATH
being set.
Go Workspaces
Common Layout
It is common to lay out 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.
Creating Your Workspace
Because we are explicitly setting the GOPATH
, we should create it prior
to setting it:
cd %USERPROFILE%
md go
cd go
md bin pkg src
Set The %GOPATH%
Variable
- Click the "Start" menu
- Type in
environment variables
and select "Edit environment variables for your account". - In the "User variables for <name>"
- Click "New"
- Set the "Variable name" to
GOPATH
- Set the "Variable value" to
%USERPROFILE%\go
- Click "OK"
Verifying The %GOPATH%
Environment Variable
- Click the "Start" menu
- Type in
cmd
and press "enter". - In the terminal type
echo %GOPATH%
and press "enter". - 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
:
- Click the "Start" menu
- Type in
environment variables
and select "Edit environment variables for your account". - In the "User variables for <name>"
- Select the
Path
variable - Click "Edit"
- Click "New"
- Type
%GOPATH%\bin
- Click "OK"
- Click "OK" again to dismiss the environment variables window
Verifying The %PATH%
Environment Variable
- Click the "Start" menu
- Type in
cmd
and press "enter". - In the terminal type
echo %PATH%
and press "enter". - 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.
Exercise: System Check
Close and reopen your terminal to ensure that all environment variables are refreshed.
Create a file called main.go
and put the following content in it:
(the file can be located anywhere on your system and will still work for this test)
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!")
}
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!")
}
Run the following command from the directory you placed the main.go
file in:
$ go run main.go
If you see anything other than Success
, your GOPATH
is not correctly set up.
Installing Git
Download Git Installer
Git is a popular SCM (source code management) tool that is used heavily by Go projects.
- Navigate to http://git-scm.com/download/win
- Download the 64-bit git for windows setup installer.
- Once downloaded Windows should prompt you to run the installer. Click
Run
. - Click through all the "next" prompts.
Verifying The Git Installation
- Click the "Start" menu
- Type in
cmd
and press "enter". - In the terminal type
git --version
and press "enter". - 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.
-
The
x
's in the version number is a placeholder for the most recent version number.
Configuring Git
- Click the "Start" menu
- Type in
cmd
and press "enter". - In the terminal type
git config --global user.email "<your email address>"
and press "enter". - In the terminal type
git config --global user.name "<your name>"
and press "enter". - 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:
Installing Visual Studio Code (optional)
Download Visual Studio Code
Visual Studio Code, or VS Code, is a great all purpose editor, and it has great support for Go.
- Navigate to http://code.visualstudio.com/download.
- Select the Windows installer.
- Once downloaded Windows should prompt you to run the installer. Click
Run
. - Click through the "next" prompts until the "Select Additional Tasks" prompt.
- On the "Select Additional Tasks" prompt select all options.
- Click "OK" to finish the installation.
Configuring Visual Studio Code (optional)
- Open Visual Studio Code
- In the menu navigate to
View
and thenCommand Palette
. - In the command palette type the following
Extensions: Install Extensions
. Click on the matching result. - Install the vs.code plugin
- Close and re-open Visual Studio Code.
- In the command palette type the following
Go install
and click onGo: Install/Update Tools
Installing Optional Components
Install A GCC (optional)
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.
- Navigate to http://tdm-gcc.tdragon.net/.
- Download the TDM64 bundle.
- Once downloaded Windows should prompt you to run the installer. Click
Run
. - Select
Create
in the installer prompt. - Select
MinGW-w64/TDM64
bundle. - Click through the "next" prompts until you get to the "Choose Components" page.
- On the "Choose Components" page, select
Install
.
Verify The GCC Installation
- Click the "Start" menu
- Type in
cmd
and press "enter". - In the terminal type
gcc
. - 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 PostgreSQL (optional)
PostgreSQL is a great, free, full-featured relational database that is very popular.
- Navigate to http://postgresql.org/download/windows.
- Click "Download the graphical installer from BigSQL".
- Select the PostgreSQL 9.x.x windows installer
- Once downloaded Windows should prompt you to run the installer. Click
Run
. - Click through the "next" prompts until the "Select Components" prompt.
- On the "Select Components" page check all the options.
- Set the password to
postgres
and then click "Next". - On the "pgDevOps" screen fill in your information.
- Click through all the "next" prompts.
-
The
x
's in the version number is a placeholder for the most recent version number.
SQLite3 (Optional)
SQLite is a file based, relational database.
- Navigate to http://www.sqlite.org/download.html.
- Go to Precomiled Binaries for Windows.
- Download the 64-DLL zip file
- Open the zip file by double clicking on it.
- Copy
sqlite3.dll
toC:\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.
- Navigate to http://nodejs.org/en/download
- Click on the Windows installer.
- Once downloaded Windows should prompt you to run the installer. Click
Run
. - Click through all the "next" prompts.
Verifying Node/NPM Installation (optional)
- Click the "Start" menu
- Type in
cmd
and press "enter". - In the terminal type
node --version
. - 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.
- Navigate to http://docker.com/docker-windows
- Click "Download from Docker Store".
- Click "Get Docker"
- Once downloaded Windows should prompt you to run the installer. Click
Run
. - Click through all the "next" prompts.
Troubleshooting
VPN Issues
Many VPN's block access to sites like github.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.