Golang Quickstart with Homebrew (MacOS)

Devesu
4 min readApr 13, 2020

--

Before starting this tutorial just want to mention I myself also very new in Go programming. I am just creating this tutorial to help newbies like me to setup and start Golang programming quickly. Frankly I just fell in love with Golang because of it’s simplicity, and I guess you will too. :-)

Sadly steps shown in this tutorial is mainly for MacOS & Linux OS. If you are a Windows user you may follow this tutorial —

So lets Go ..

We are going to use Homebrew to install Golang, so Install Homebrew in your machine if you don’t have it.

  1. Install Golang using Homebrew

Open Terminal run below command -

$ brew update

once done then -

$ brew install golang

2. Create a Go workspace folder

Normally people create a folder as “go” inside their home folder.

$ mkdir $HOME/<your_go_workspace_folder_name>

3. Setup environment

Open your shell config file (.bashrc or .zshrc) file. If you are not sure which shell you are using then on your terminal use below command -

$ echo $SHELL

Now open your shell config file (for me its .zshrc file) using this command —

$ vi ~/.zshrc

Shell config file is located inside your home folder, that’s the reason we are using “~/” before file name.

If you are not familiar with vi editor then check below link for vi editor commands —

Now add below lines inside your shell config (there may be other existing commands inside that file).

export GOPATH=$HOME/<your_go_workspace_folder_name>export GOROOT=/usr/local/opt/go/libexecexport PATH=$PATH:$GOPATH/binexport PATH=$PATH:$GOROOT/bin

Replace the bolded text with your own Go workspace folder name.

To save the file and exit type :wq and press enter.

Now all setup are done. Let’s write a simple program to test our Go installation.

Create a project folder as “TestApp” at any location. And then create a file inside that folder as “main.go”. Add below lines of code inside main.go file —

package mainimport ("fmt")func main() {fmt.Println("Running the TestApp")}

Now from Terminal navigate to your “TestApp” project folder and execute below command to run our Go program —

$ go run main.go

It should display an output like this —

Great our first Go program is working now.

Let’s try to understand what we did till now.

In step 1 we installed Golang using brew.

In step 2 we setup 2 path variables GOROOT and GOPATH.

GOROOT — It contains location of Go SDK

GOPATH — It defines location of Go workspace. So if we install any 3rd party library all it’s files will be stored inside our $GOPATH/bin, $GOPATH/pkg and $GOPATH/src folder, and since we already defined GOPATH, our programs can use those libraries directly same like other standard Go libraries.

Let’s try to install a 3rd party library and check it. In Go we install any library using “go get” command.

Try to execute below command on terminal —

$ go get github.com/Pallinder/go-randomdata

After successful execution of the command check your Go workspace folder, you should see something like this.

In current version Go comes with dependency management system (go mod) which eliminates our dependency on GOPATH. To understand basic of “go mod” checkout this article.

Go is a very interesting language, I am sure you will enjoy it.

happy coding…

--

--