-
Notifications
You must be signed in to change notification settings - Fork 1
Setup Golang
- Overview
- Installing GO
- The $GOPATH
- Go Workspaces
- Common Layout
- Creating Your Workspace
- Setting Up The $GOPATH
- $GOPATH Is Not $GOROOT
- Add Go Bin Directory To $PATH
- Exercise: System Check
- Revision Control Systems
- Editors
- VPN Issues
This course will show you how to set up a Mac/Linux machine for modern software development with Go.
Core Steps:
Install Go
Set up environment variables such as GOPATH and adding BIN to your existing PATH
Optional Steps:
Install and configure Revision Control Systems
Install and configure popular code editors
Download the latest installer from go: https://go.dev/doc/install
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.
Under the $GOPATH there are three folders:
.
├── bin
├── pkg
└── src
- This is where compiled Go programs will be installed.
- Compiled package objects. You can safely ignore this directory.
This is where all of your source code for Go projects has to live.
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.
Because we are explicitly setting the GOPATH, we should create it prior to setting it:
mkdir -p $HOME/go/{bin,pkg,src}
In your .bash_profile, or equivalent file:
export GOPATH="$HOME/go"
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 /usr/local/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.
For convenience, add the workspace's bin subdirectory to your PATH:
NOTE: If you used an installer, you typically do not need to do this step.
Instructions for Linux or Mac
export PATH=$PATH:$GOPATH/bin
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!")
}
file: ./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: ./src/env_check.go
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.