How to install Golang environment with a Mac ?

Go (golang programming language)

Install Golang with Homebrew:

$ brew update


It takes a while to pull all the latest ... be patient ...

$ brew install golang



Go environment

$ vi .bashrc

Then add those lines to export the required variables

This is actually your .bashrc file

export GOPATH=$HOME/go-workspace # don't forget to change your path correctly!
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

Create your workspace:

Create the workspace directories tree:

$ mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin

$GOPATH/src : Where your Go projects / programs are located
$GOPATH/pkg : contains every package objects
$GOPATH/bin : The compiled binaries home

source ~/.bashsrc

Format your Go code

Go has a tool that automatically formats Go source code.

$ gofmt -w yourcode.go

OR

$ go fmt path/to/your/package

Go version

When installed, try to run go version to see the installed version of Go.

davidr$ go version
go version go1.13.4 darwin/amd64