How to Install Golang on MacOS?
Last Updated :
02 Dec, 2022
Before, we start with the process of Installing Golang on our System. We must have first-hand knowledge of What the Go Language is and what it actually does? Go is an open-source and statically typed programming language developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009. It is also known as the Golang and it supports the procedural programming language. It was initially developed to improve programming productivity of the large codebases, multicore, and networked machines. Golang programs can be written in any plain text editor like TextEdit, Sublime Text, or anything of that sort. One can also use an online IDE for writing Golang codes or can even install one on their system to make it more feasible to write these codes. Using an IDE makes it easier to write Golang codes because IDEs provide a lot of features like intuitive code editor, debugger, compiler, etc. In this article, we have covered the following topics:
Steps for Installing Golang on MacOS
Step 1: Checking if Go is installed or not. Before we begin with the installation of Go, it is good to check if it might be already installed on your system. To check if your device is preinstalled with Golang or not, just go to the Terminal and run the command:
go version
If Golang is already installed, it will generate a message with all the details of the Golang version available as shown below, otherwise, it will give an error. Step 2: Before starting with the installation process, you need to download it. So for that, all versions of Go for MacOS are available on https://golang.org/dl/ Download the Golang according to your system architecture. Here, we have downloaded go1.13.1drawin-amd64.pkg for the system. Step 3: After downloading process, install the package on your system: Step 4: After completing the installation processes. Open a program known as Terminal(It is a command-line interface for macOS) to check whether Go install properly or not using the Golang version command. As shown in the below image, here it shows the version information of the Golang which means Go install successfully in your system.
Setting up the Go Workspace
After successfully installing Go on your system, now we are going to set up the Go workspace. Go workspace is a folder on your computer where all your go code will going to store. Step 1: Creating a folder named as Go in documents(or wherever you want in your system) like as shown in the below image: Step 2: Now tell the Go tools to where to find this folder. So for that first of all, you go to your home directory using the following command:
cd ~
After that set path of the folder using the following command:
echo "export GOPATH=/Users/anki/Documents/go" >> .bash_profile
Here, we are adding export GOPATH=/Users/anki/Documents/go to .bash_profile. The .bash_profile is a file that automatically loaded when you logged into your Mac account and it contains all the startup configurations and preferences for your command-line interface(CLI). Step 3: Now check that your .bash_profile contains the following path using the following command:
cat .bash_profile
Step 4: Now we are going to check our go path using the following command. If you want to skip this step you can do that also.
echo $GOPATH
Creating first Golang program
Step 1: Download and install a text editor according to your choice. After installation, create a folder named go(or whatever name you want) in Documents(or wherever you want in your system). In this folder, create another folder named as source and in this source folder create another folder named as welcome. In this folder, all your go programs are going to be stored. Step 2: Let’s create first go program. Open the text editor and write go program as shown below: Step 3: After creating a go program save that program with extension .go. Step 4: Now open terminal to run your first go program. Step 5: Change the directory in which your program is stored. Step 6: After changing directory, run the go program using the following command:
go run name_of_the_program.go