Installing Go for first-time programmers on a Mac
If you’re interested in learning the Go programming language, this guide will walk you through how to set up a brand new Mac step-by-step, with all the tools you’ll need to write and run Go programs.
In this guide will cover how to:
- Download & Install Go; the programming language tools/binaries.
- Download & Install a Code Editor; a tool to write Go code in.
- Create Your Go Workspace; a place to store your code.
- Write Your First Go Program; a simple “hello world” program.
Let’s begin!
1. Download & Install Go
Installing Go
Download Go for “Apple macOS” from: https://golang.org/dl/
Double-click on the downloaded file (e.g. go1.10.2.darwin-amd64.pkg
) to run the installer:
Click “Continue”, click “Install”, enter your password and press Enter or Click “Install Software”. Go will now begin installing, and after a moment you should see a green tick:
Verifying Go is installed
You might be wondering how do you “use” Go? Welcome to the world of the Terminal (a.k.a. “command-line” or “shell”).
To open the Terminal on a Mac, open Spotlight search (use the Command-Tab [⌘-Tab] shortcut, or click the search icon [🔍] at the top-right of your screen), then search for “Terminal”.
If Terminal is highlighted (as above), press Enter — this should open a new Terminal window. Type go version
to verify Go is installed correctly:
The go1.10.2 darwin/amd64
line has the same information in the filename of the file we downloaded when installing Go — this confirms we’ve got that version correctly installed.
Note: if you see an error like bash: go: command not found
then something is broken with your Go installation. At this point, asking a friend, searching the web or asking for help (e.g. on Gophers Slack) is probably the best way to get helping fixing things — we have a very friendly community, please don’t be afraid to ask for help! :)
2. Download & Install a Code Editor
Overview of code editors for Go
There are a huge number of code editors and IDE’s that you can choose from to make writing Go code as easy and enjoyable as possible. A good editor will highlight issues with your code, make browsing code easier, will provide auto-completion and offer hints/additional context for the code you’re working on.
For writing Go, here is a list of popular options:
- Visual Studio Code with the Go extension
- Vim with the vim-go plugin
- Atom with the go-plus plugin
- Sublime Text with the GoSublime plugin
- GoLand
Note that the first three are Free and Open Source — the last two are not.
This guide covers using Visual Studio Code (a.k.a. “VS Code”) because I’ve found it the easiest for beginner users to get up-and-running, it’s free and it’s Open Source.
Installing VS Code
Download for Mac from https://code.visualstudio.com/Download
Double-click to unzip the VSCode-darwin-stable.zip
file, and drag the Visual Studio Code
application into your Applications
folder.
Once you’ve moved it to the Applications folder, launch VS Code by double-clicking the Visual Studio Code
application (you can also launch using the Spotlight search feature we used earlier — searching for “vs” should find it).
See also: official setup guide for macOS.
A note about Telemetry in VS Code
One thing I always like to tell people when suggesting they install VS Code is that it collects telemetry data which Microsoft uses “to help understand how to improve the product. For example, it helps debug issues such as slow start-up times and prioritize features”. And while you might be ok with this, I think it’s important to show you how to disable Telemetry Reporting and Crash Reporting, in case you want to do that.
Both Telemetry Reporting and Crash Reporting can be switched off by toggling two simple settings:
"telemetry.enableTelemetry": false
"telemetry.enableCrashReporter": false
To do this, open VS Code, click on “Code” menu at the top of the screen, click Preferences -> Settings to open Settings (or use the Command-Comma [⌘ ,] shortcut). Now in the search bar, type “telemetry”:
To toggle the telemetry settings, click on the edit icon (✎) next to each setting and select false;
Note: when you change the Crash Reporter setting, you’ll be asked to restart VS Code — click “Restart” and the changes should be saved.
Your “User Settings” should now look like this:
Hooray for privacy!
See also: official guide for how to disable telemetry reporting
Installing the Go extension
The final step to getting VS Code ready for your Go code is to install the “Go” extension. This enables VS Code to understand the Go language better..
To go to the “Extensions” view, either click the bottom-left icon on the left-hand-side of the editor, or click on the “View” menu at the top of your screen and select “Extensions” (or use the Shift-Command-X [↑⌘X] shortcut).
From the Extensions view, search for “go” and the first result should be the official vscode-go extension. Click the green “Install” button, and after a minute a “Reload” button should appear — click “Reload” and VS Code will restart. If you navigate to the Extensions view and don’t have anything in the search bar, you should now see the Go extension installed:
Congratulations — you’re editor is all set-up!
3. Create Your Go Workspace
Your code will need to live in a file somewhere. Most programming languages don’t care where this exists on your computer. Go has the concept of a “Workspace”; a directory where all of your Go code should live.
Since Go version 1.8, the Workspace defaults to a directory called “go” inside your home directory. You can override it by setting your GOPATH — for beginners I’d recommend sticking with the default.
If you’re wondering how to access your “home directory” in Finder, you can add it to the Favorites list on your Finder sidebar by going to Finder -> Preferences, Sidebar tab:
You should now see this in the list of Favorites in the Finder sidebar, and you can drag it to the top of the list for easy access. If you browse to your home directory, you should now see the “go” workspace directory:
As you learn about the concept of the Workspace, you’ll also learn the basics of Packages in Go — neat!
Let’s use an example: https://github.com/ryan0x44/go-helloworld
This is a Go package called “go-helloworld”. You can download & install it by running this in your terminal: go get github.com/ryan0x44/go-helloworld
Running go get
should automatically create your Workspace in the default location, and download the contents of that Github link to src/github.com/ryan0x44/go-helloworld
in your home directory — which would look like this in Finder:
Your home directory is the same location a Terminal window opens into by default. So if you launch a new Terminal, and use the cd
command to “change directory” and the ls
command to “list files”, you can see the same directory structure as in Finder:
Note that you can “cd” one directory/level at a time, or multiple levels as per the last line of the screenshot.
You can now open this code up in Visual Studio Code. Click on the “File” menu, and click “Open…” (or use the Command-O [⌘O] shortcut) — then select the “src” directory inside your Go Workspace.
You should be able to use the arrows to expand the full hierarchy of directories much like Finder, as seen in the screenshot above.
If you double-click on the main.go
file, VS Code might prompt you to install some additional Go tools — just click “Install All”.
You should then see the main.go file, e.g:
You can run this program from the Terminal really easily.
- Change to the package directory,
e.g.cd ~/go/src/github.com/ryan0x44/go-helloworld
- Use
go run
to execute the code,
e.g.go run main.go
The program should output “Hello world!”:
Great — now it’s time to start writing some code of your own!
4. Write Your First Go Program
You now know how the Workspace is structured and how to run a single file. Note that when you create your own programs, you can create packages without using GitHub e.g. hello-you
can be your package name and you can place it under go/src/hello-you
.
Using Finder or Terminal or VS Code, create a new directory under src
called hello-you
— to do this with the Terminal open a new window and type mkdir go/src/hello-you
.
Now create a file called main.go
under go/src/hello-you
with the following:
package mainimport "fmt"func main() {
name := "you"
fmt.Printf("Hello %s", name)
}
This creates a variables called name
with the value of “you”. The %s
that you see in the first parameter of Printf
is going to take the second parameter (our name
variable), and print that as a string (the s in %s
means “string”). See https://golang.org/pkg/fmt/ for more information about formatting options.
Try running building this program and running it in your Terminal:
cd go/src/hello-you
go build
./hello-you
You should see “Hello you”, or if you change the value of the name
variable, then whatever that value was.
Running go build
created a binary file called hello-you
which you execute using ./hello-you
. You can also use the go install
command to have this file installed in your go/bin
directory:
cd go/src/hello-you
go install
~/go/bin/hello-you
A nice homework project is to learn how to add your Workspace “bin” directory to your PATH environment variable (see the official Go Getting Started guide for help!) — once you’ve done that, any of the binaries in your go/bin
directory can be executed e.g. using hello-you
no matter which directory your Terminal is in. Neat!
From here, if you haven’t already done so I’d highly recommend following A Tour of Go. As you learn new things, you can try them out in new packages/files.
You might want to also learn Git, which will help you keep backups and manage versions of your work, as well as allowing you to share it and collaborate on it with others.
Congratulations! You now have all the tools you need to start building amazing software in Go — we can’t wait to see what you create!