I spent substantial amount of time when first setting up my Go development environment on macOS Sierra Version 10.12.3 as it is not as straight forward as I expected it to be. After going through numerous hiccups, I finally managed to get it done. So, I would like to share my experience with new Go developers to help them speed up their preparation process.
- Download and install Go via
- Test your Go installation
- Prepare your Go workspace
- Download and install gdb via
- Configure gdb
- Test your gdb installation
You may choose to download and install Go via official binary distribution, Homebrew or Go Version Manager (GVM). This guide will walk you through using the official binary distribution.
Download the Mac OS X package installer, open it and follow the prompts to install the Go tools. The package installs Go to /usr/local/go
and exports /usr/local/go/bin
directory in your PATH environment variable.
Restart any open Terminal sessions for changes to take effect. Enter go version
in Terminal and it should show: go version go1.8 darwin/amd64
.
A Go workspace is a directory hierarchy with three directories at its root:
src
contains Go source filespkg
contains package objectsbin
contains executable commands
You may place your Go workspace anywhere. This guide will use the following as example: $HOME/Development/go
.
Enter mkdir -p $HOME/Development/go/{bin,pkg,src}
in Terminal and it shoud create your workspace directories. You need to tell Go to use this as your default workspace by exporting it as GOPATH variable.
Enter vi $HOME/.bash_profile
in Terminal and append the following line to it: export GOPATH=$HOME/Development/go
, then save and exit. Restart any open Terminal sessions for changes to take effect.
Enter go env
in Terminal and verify that GOPATH is pointing to $HOME/Development/go/
.
Download and install gdb via Homebrew by entering brew install gdb
in Terminal. Restart any open Terminal sessions for changes to take effect.
Before you can use gdb for debugging Go, you need to perform 2 prerequisites:
- Create code signing certifacte for gdb
- Create
.gdbinit
script for gdb
- Open Keychain Access application (can be found in Applications/Utilities directory or through Spotlight). Click on Keychain Access > Certificate Assistant > Create a Certificate... in the application menu.
- Enter
gdb-cert
as Name, selectSelf Signed Root
as Identity Type, selectCode Signing
as Certificate Type and checkLet me override defaults
- Enter
3650
as Validity Period (days) (Maximum validity period is 20 years) - Keep clicking on Continue to skip the next six screens until you see the one entitled Specify a Location For The Certificate
- Select
System
as Keychain and click on Create (If prompted, type in your password) - Click on System under Keychains in the sidebar on the left of Keychain Access application
- Double click on
gdb-cert
from the list - Select
Always Trust
as When using this certificate by expanding the Trust section in the information window that appear - Enter
killall taskgated
in Terminal to restart the Taskgate access-control service - Enter
codesign -s gdb-cert /usr/local/bin/gdb
in Terminal and enter your password if prompted. gdb is successfully signed if there is no ouput in Terminal
Enter vi $HOME/.gdbinit
in Terminal and append the following line to it: set startup-with-shell off
, then save and exit. Restart any open Terminal sessions for changes to take effect.
You may test your gdb installation by creating a simple Go program, adding breakpoints to it and start debugging.