Setting up a Go Development Environment | Mac OS X

Go is quickly growing in popularity, so I thought it would be helpful to show you how to run Go programs on your machine.

If you haven’t set up a general(not specific to Go) development environment at all on your machine so far, please walk through this article to get up to speed(FYI I’ll be walking through setting up the VS Code text editor in this article, so there’s no need to install Atom unless you’d like to compare the two):

Prerequisites: Home Brew and a github.com account

Let’s get started.

Installing Go via the Terminal

Open your terminal via spotlight(press command + space then type “terminal” and press enter) or by finding the application in finder.

If you’ve already installed Home Brew type the following command in the terminal and press enter:

brew install go

After Go has finished installing, we’ll need to create a workspace specifically for Go code.

If you have a workspace folder that you use for development, you’ll want to create the following directory within that folder.

Create your Go folder via the terminal:

mkdir Go

Change directory into your Go folder:

cd Go

Go requires a specific folder structure in order to manage your local packages and your code.

From within the Go folder enter the following commands:

mkdir bin pkg src
mkdir src/github.com
mkdir src/github.com/your_github_username

In order to run Go within your terminal, you’ll need to setup the GOPATH environment variable. This will allow Go to properly locate your workspace in order to run your code and manage your packages.

Personally, I use the Oh My Zsh terminal shell, but that won’t be the case for everyone.

For those of you using the Oh My Zsh terminal shell, you’ll want to edit the ~/.zshrc file.

If you are using a different terminal shell you’ll have to find the file your shell uses for terminal aliases.

For the others who aren’t using any terminal shells, you’ll want to edit your ~/.bash_profile file(if it doesn’t exist for you, you’ll need to create it using touch ~/.bash_profile).

Now we need to edit the file appropriate for aliases in your terminal to contain the GOPATH variable.

Using vim, open the appropriate file(for me this is the ~/.zshrc file):

vim ~/.zshrc

You’ll now be in the vim terminal based text editor and may see output similar to this(if using Oh My Zsh):

In order to edit this file, you’ll need to press “i” to enter “insert mode”.

You should now see this at the bottom of the screen:

Hold the down arrow until you reach the bottom of the file.

Enter the following text, be sure to adjust the file path depending on whether you put the Go folder within a workspace folder, or simply put it in the root/home “~” directory of your terminal. Keep in mind that $HOME is equal to “~” AKA /Users/your_username.

export GOPATH=”$HOME/workspace/Go”

After adding that text to your file, press the escape key(esc) to exit insert mode, then press shift ; and enter “wq”(which stands for write + quit which saves the changes and quits the editor) like so:

Press enter and you should be moved back to your command line(terminal).

Close and restart your terminal in order to have the alias changes take effect.

In your terminal, type the following command:

go

You should see an output such as this:

Now change directory(cd) into your Go workspace and go the github.com folder relating to your GitHub account name:

cd ~/workspace/Go/src/github.com/your_github_username

OR

cd ~/Go/src/github.com/your_github_username

Let’s create a Hello World Go program and make sure you can run it in your terminal.

Enter the following commands:

mkdir hello_world
cd hello_world
touch main.go

Open your main.go file in vim:

vim main.go

Press “i” to enter insert mode and then type the following code(don’t copy and paste the quotation marks(“”), you will need to type those yourself within the editor or you’ll receive an error):

package main
import “fmt”
func main() {
fmt.Println(“Hello World!”)
}

Press esc then shift; then “wq” and finally press enter to finish editing the file:

Back in the hello_world folder, you should be able to type the following command and get the proper output:

go run main.go

The proper output would be:

If you received that output, you have properly setup your Go workspace on your machine.

Editing Go using the vim text editor is not optimal, therefore I will now show you how to use VS Code as a Go text editor to enhance your development productivity/experience.

Setting up the VS Code text editor for Go

To get started, let’s install VS Code from this site:

Don’t forget to click the installed zip folder and drag VS Code from the Downloads folder to the Applications folder to complete the installation.

Now let’s open VS Code via spotlight. Press command + space and type “VS Code” then press enter.

When VS Code opens, press command + shift + p and type “shell”.

Select the top option “Shell Command: Install ‘code’ command in PATH”.

Now you should be able to open folders/files in VS Code from the terminal.

Let’s try it out in the terminal, back in your ~/workspace/Go/src/github.com/your_github_username folder run the following command(code + space + c):

code .

This will open the current folder in VS Code.

You should now see the main.go file and hello_world folder you created in the top left of the application(If you’re wondering about the Go icon next to the file, I’m using the material icons theme which you can install in the extensions section of VS Code):

Let’s toggle on autosave before we forget. That way your editor will automatically save the changes you make to your files. Click “Auto Save” in the file dropdown at the top of the screen.

Go to the extensions section of VS Code and let’s install the Go package and press the restart button shown.

Now let’s go back to our file

And then double click your main.go file to open it up.

Either right away, or after making some simple changes to the file, you should seem some options such as these:

Click “Install All” for all the suggestions and let those packages finish installing.

If any errors occur in the installation output at the bottom of the editor, follow the given instructions until all issues have been addressed.

You now have a working Go text editor.

Go tips:

If you ctrl + click any of the package methods such as fmt.Println and choose the “Go To Definition” option, you’ll be shown the documentation for that method directly in your editor in a new tab.

If you would like to learn the Go programming language I would highly recommend this course:

If you would like to learn how to use the Go programming language for web development I would highly recommend this course:

Happy coding :)