How to install Go and set up a local development environment on macOS

0 Shares
0
0
0
0

Introduction

Go is a programming language born out of frustration. At Google, developers were tired of having to make trade-offs when choosing a language for a new project. Some languages run efficiently but take a long time to compile, while others were easy to write but inefficient in production. So Google invented Go, designing the language to have it all: fast to compile, fast to run, easy to write, and easy to deploy.

While Go is a versatile language that can be used for many types of projects, from web applications to command-line tools, it is particularly well-suited for distributed systems and microservice architectures, and is known as a cloud language. It helps the modern programmer with a robust set of tools, eliminating the formatting discussions that are part of the language specification, and also simplifies deployment by compiling each program and all its dependencies into a single binary. Go is easy to learn, with a very small set of keywords, making it a great choice for both novice and veteran developers.

In this introductory tutorial, you will install Go on your local macOS machine and run your first application to prove that the installation worked.

Prerequisites
  • You need a macOS computer with administrative access that is connected to the internet.

Step 1 – Open Terminal

The macOS Terminal is an application that you can use to access the command line interface. You can find it by going to the Finder, navigating to the Applications folder, and then to the Utilities folder. From here, double-click Terminal.

Now that you have the terminal open, you can download and install Xcode, the developer tools package you need to install Go.

Step 2 – Install Xcode

Xcode is an integrated development environment (IDE) that includes software development tools for macOS. You can check if Xcode is already installed by typing the following in Terminal:

xcode-select -p

The following output means that Xcode is already installed:

Output
/Library/Developer/CommandLineTools

If you get an error, install Xcode from the App Store and accept the default options.

Once Xcode is installed, return to your terminal window. Next, you need to install the separate Xcode Command Line Tools application, which you can do by typing:

xcode-select --install

At this point, Xcode and its Command Line Tools application are fully installed and you are ready to install the Homebrew package manager.

Step 3 – Installing and Setting Up Homebrew

While the macOS terminal is very similar to Linux terminals and other Unix-like systems, it doesn't come with an official command-line package manager like Linux distributions. A package manager helps you install, upgrade and configure software, and uninstall it, either interactively from a terminal or within a script. There are several open source (and unofficial) package managers for macOS, and Homebrew has emerged as one of the most popular. It offers a quick and flexible way to install and update Go on macOS.

To install Homebrew, run this in the terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

This command will download a script from GitHub and install Homebrew. If you want to enter your password, note that your keystrokes will not be displayed in the terminal window, but they will be recorded. Just press the return key after entering your password. Otherwise, press y for "yes" whenever you are asked to confirm the installation.

Once the installation is complete, you'll place the Homebrew directories at the top of your PATH environment variable so that anything you install via Homebrew takes precedence over any similarly named programs that come pre-installed on macOS (if any). Since macOS doesn't come with Go, putting Homebrew at the top of your PATH isn't strictly necessary in this case, but for the sake of consistency, many developers prefer to add Homebrew to the top of their PATH.

To do this, create or open the ~/.zprofile file with the nano command-line text editor:

nano ~/.zprofile

Note: If you're running a version of macOS older than Catalina 10.15, your Terminal probably uses the Bash shell (/bin/bash) instead of the Z-shell (/bin/zsh). In that case, you'll need to create or open the ~/.bash_profile file instead of ~/.zprofile. To check which shell you're using, run echo $SHELL.

Add the following line to the file:

eval "$(/opt/homebrew/bin/brew shellenv)"

Exit nano by typing CTRL+x and when prompted to save the file, type y and then press ENTER.

Now enable these changes:

source ~/.zprofile

You can verify that Homebrew was successfully installed by typing:

brew doctor

If no update is needed at this time, the output will be as follows:

Output
Your system is ready to brew.

Otherwise, you may receive a warning to run another command, such as brew update, to ensure your Homebrew installation is up to date.

Once Homebrew is ready, you can install Go.

Step 4 – Install Go

You can search for all available Homebrew packages with the brew search command. For the purpose of this tutorial, you will search for Go-related packages or modules:

brew search golang

Note: Avoid running brew search go as it will return too many results. The Go language is often called Golang, so use golang as the search term to narrow down the results.

The terminal will display a list of items you can install:

Output
golang golang-migrate golangci-lint glslang

You want the first result: Glang. Install it now:

brew install golang

The installation may take a few minutes. When it's done, check the version of Go you have installed:

go version

Homebrew should install the latest stable version of Go. At the time of writing, that version is 1.19.4.

To update Go in the future, you can run these two commands to first update Homebrew and then Go: (You don't need to do this now, as you just installed the latest version.)

brew update
brew upgrade golang

brew update updates Homebrew's own formulas, ensuring that you have the latest information about the packages you want to install. brew upgrade golang updates the golang package to the latest version.

With Go installed, you are ready to compile and run your first program.

Step 6 – Writing Hello World in Go

This section doesn't explain anything about Go programming. The goal is just to compile and run the simplest program you can imagine to convince yourself that Go works.

From your home directory, create a new file using a text editor like nano:

nano hello.go

Paste into this program:

package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}

Exit nano by typing CTRL+x and when prompted to save the file, type y and then press ENTER.

Then compile and run the program with this command:

go run hello.go

You should see this output:

Output
Hello, World!

Go is live! You're ready to begin your adventures in Go.

Result

This tutorial provided the most concise introduction to the Go programming language. You installed Go and ran your first program.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like