Skip to main content

Installing Go on macOS

This tutorial will guide you step-by-step on how to install Go (also known as Golang) on a macOS system. Go is a statically typed, compiled language, known for its simplicity and efficiency. It's widely used in production at Google and in many other organizations and open-source projects.

Prerequisites

Before you start, make sure you have a macOS system with administrative access and a stable internet connection to download the necessary files.

Step 1: Download Go

First, navigate to the Go Downloads page at https://golang.org/dl/. Select the version of Go you want to download.

For macOS, you should download the Apple macOS .pkg file. Click on the link to start the download.

Step 2: Install Go

Once the download is complete, click on the .pkg file. This will initiate the installation process.

You'll be prompted with a welcome message. Click on 'Continue' to proceed.

After reading the software license agreement, click on 'Continue', and then 'Agree' to accept the terms of the agreement.

Select the disk where you want to install Go, then click 'Continue'.

Finally, click 'Install' to start the installation. You may be asked for your password. Enter it to authorize the installation.

Wait for the installation to complete, then click 'Close'. You can move the installer to the trash if you're asked.

Step 3: Verify the Installation

Now, it's time to verify that Go has been installed correctly.

Open a new Terminal window and type the following command:

go version

You should see the version of Go that you've just installed, which confirms that Go is installed correctly.

Step 4: Set Up a Workspace

Go requires you to have a workspace which is a directory where you'll store your Go code.

By default, Go assumes your workspace is located at $HOME/go. If you want to use a different location as your workspace, you need to set the GOPATH environment variable to point to your workspace directory.

To do that, open the Terminal and type:

export GOPATH=$HOME/path/to/your/workspace

Don't forget to replace path/to/your/workspace with the actual path to your workspace directory.

Now, every time you start a new Terminal session, GOPATH will be set to your chosen workspace directory.

Step 5: Test Your Setup

Let's write a simple Go program to test your setup.

In your workspace directory, create a new file named hello.go and open it in a text editor.

Write the following Go code:

package main

import "fmt"

func main() {
fmt.Println("Hello, world!")
}

Save the file and close the text editor.

Now, go back to the Terminal and navigate to your workspace directory using the cd command.

To run your Go program, type:

go run hello.go

You should see the message "Hello, world!" printed in the Terminal. This means you've successfully set up your Go environment and are ready to start developing in Go!

Conclusion

Congratulations! You've successfully installed Go on your macOS system, set up your Go workspace, and run your first Go program. Now you're ready to explore more of what Go has to offer. Happy coding!