Installing Golang on Mac is very easy. You can download the binary from the official website or install it using homebrew. Golang provides binary distributions for Mac OS X, Windows, and Linux. Then set up your Golang environment. You can download the Go source code and install it from the source using a different OS.
How to Install Golang On Mac
- Download the latest version of Go for your platform here: https://golang.org/dl/.
- Follow the instructions for your platform to install the Go tools: https://golang.org/doc/install#install. It is recommended to use the default installation settings.
- On Mac OS X and Linux, Go is default installed to directory /usr/local/go, and the GOROOT environment variable is set to /usr/local/go/bin.
- You may need to restart any open Terminal sessions for the change to take effect.
- Also, we need to install the VSCode, because some tools we will install inside VSCode will help us during the Golang project development.
Now, Create a go file named hello.go and add the following code.
/* hello.go */ package main import "fmt" func main() { fmt.Printf("Welcome to GO AppDividend") }
Okay, now you can run the file using two ways.
First, you can compile the file using the following command.
go build
It will create an executable file with the same file name; if your filename is hello.go, then it will create a hello file. On MAC, it is just a hello file; in windows, it is a hello.exe file.
Now, run the executable file on Mac using the following command.
./hello
It will give the output in the terminal.
There is also another way in which you get the direct output.
Type the following command in your terminal.
go run hello.go
In Visual Studio Code, our project looks like the one below.
Install Golang Tools On Mac
If upgrading from an older version of Go, you must first remove the existing version.
Linux, macOS, and FreeBSD tarballs
Download the archive and extract it into /usr/local, creating a Go tree in /usr/local/go
. For example:
tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
Choose the archive file appropriate for your installation. For instance, if you install Go version 1.2.1 for 64-bit x86 on Linux, the archive you want is called. go1.2.1.linux-amd64.tar.gz
.
(Typically, these commands must be run as root or through sudo
.)
Add /usr/local/go/bin
to the PATH
environment variable. You can do this by adding this line to your /etc/profile
(for a system-wide installation) or $HOME/.profile
:
export PATH=$PATH:/usr/local/go/bin
Note: Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as source $HOME/.profile
.
That’s it for this tutorial.