How to setup Go on Mac using Homebrew

In this post I will be showing how to install Go on Mac using Homebrew. Pre-requisite for installing go in this demo is that you have Homebrew installed. Let’s get started.

step 1: Install Go with Homebrew

tunde:~ babatundeolu-isa$ brew update
tunde:~ babatundeolu-isa$ brew install golang

step 2: Now that we have Go installed, let us confirm the version of Go installed

tunde:~ babatundeolu-isa$ go version
go version go1.11 darwin/amd64

step 3: let us setup our workspace.

tunde:~ babatundeolu-isa$ mkdir Aquatribe-go
tunde:~ babatundeolu-isa$ cd Aquatribe-go/

Next we will create the bin ,pkg and src directories. the bin directory will contain all compiled binaries, pkg will contain Go package objects and src will contain our Go projects

tunde:Aquatribe-go babatundeolu-isa$ mkdir -p src pkg bin

step 4: We will set up the Environmental variables needed for our Go projects.
to do this, we will need to open the bash_profile file located in the Home directory and add the workspace we are using for our Go work.

tunde:~ babatundeolu-isa$ vi .bash_profile

next add the following lines that exports the variables for Go workspace.

#for GO Programming
export GOPATH="$HOME/Aquatribe-go"
export PATH="$HOME/Aquatribe-go/bin:$PATH"

step 5: let us try running a simple go program.
first, create the following file in the src directory and save as intro.go

package main
import "fmt"

func main() {
    fmt.Printf("Aquatribe provides solution for Containers, Severless and Cloud. \n")
}

Let us run the code.

tunde:src babatundeolu-isa$ go run intro.go
Aquatribe provides solution for Containers, Severless and Cloud.

We can also use the go install command to compile the code and save in the bin directory.

tunde:src babatundeolu-isa$ go install intro.go

For more details on Go refer to the links below.
https://golang.org
https://godoc.org
https://tour.golang.org

Leave a Reply

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