Installing Go on a Mac
As with most command line tools, the easiest way to install Go on a macOS host is to use Homebrew. You’ll need to install two packages.
brew update
brew install go
brew install gopls
You will also need Delve (dlv) if you are going to use Visual Studio Code as your IDE. Unfortunately the Homebrew package for Delve is both deprecated and broken, so you’ll need to install this a slightly different way, which we’ll come to in a minute.
Default Configuration
By default, Go expects your code to live in a “go” directory underneath your home directory, but unless you’re only using your Mac for Go software development (unlikely!) then you’ll want to put that somewhere else. In my case, I have all my Go resources in one of my GitHub repositories which is cloned into my Development directory, so I needed to replicate the standard Go directory structure there…
mkdir -p ~/Development/github/examples/golang/{src,pkg,bin}
…and then set an environment variable in my logon script (e.g. .zshrc) to tell Go where this is. For good measure I also added the bin directory to my path.
export GOPATH=~/Development/github/examples/golang
path+=($GOPATH/bin)
export PATH
Coding Tutorial
Go is fairly straightforward if you’re coming at it with a background in another programming language such as C++ or Java. If you follow the Getting Started with Go tutorial you’ll soon be up to speed. All you’ll need is a decent text editor and a command line. My versions of the tutorial sources can be found in my Examples GitHub repository. As usual I’ve experimented a little to replicate a more typical multi-module development environment.
Developing in an IDE
Unfortunately Go support in Eclipse is flaky and JetBrains charge for their GoLang IDE, so probably the next best option is to use Microsoft’s Visual Studio Code. This isn’t really a full blown IDE but it does have features like auto-complete and you can build, run and debug your code without leaving the tool.
Before you fire up VSC, you’ll need to install Delve. If you’ve followed the steps above, you should be able download and install Delve thus.
go get -u github.com/go-delve/delve/cmd/dlv
You will also need to install the Go extensions from the Visual Studio Code marketplace. You’ll be offered the opportunity to do this when you first start VSC, or you can select the bottom “four boxes” icon from the left hand toolbar to access a list of available extensions.
Visual Studio Code workspaces are a bit quirky, for example you can’t simply open your src directory and expect it to be able to build and run everything in the subdirectories. I found I had to read up on multi-root workspaces to get the IDE to work the way I wanted it to. A quick overview of the steps:
Open one of your code subdirectories, e.g. “hello”. This creates a default, untitled workspace and VSC will recognise that it contains Go code.
File -> Add Folder To Workspace
and then select your other subdirectories. This converts the workspace to multi-root.
Save the workspace to the top level src directory with a name of your choosing, e.g. go-examples.code-workspace.
A workspace is a simple JSON file so in theory there’s no problem, and indeed several advantages, in storing this file in your version control system.