Joseph Jude

Installing Golang in MacOSX

2017.11.20 / code / golang /

Install and setup Golang on MacOSX

Installing golang on MacOSX

The easiest way to install Golang on MacOSX is by using brew.

1. Install brew

Go to terminal. Issue the following command:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. Install Golang

Now install golang.

brew install go

If you are going to deploy your programs to linux based servers, you should enable cross compilation feature. If so, install using the following command:

brew install go --cross-compile-common

You can verify the installation by issuing go version. As of this writing, the latest version is 1.9.

3. Setup Go environment

Go is an opinionated language. If you want to learn Go, you have to follow its conventions.

By convention, all Go code lives within a single workspace (folder). This workspace could be anywhere in your machine. If you don't specify, Go will assume $HOME/go as the default workspace. Place this workspace in your machine anywhere you like. For me it lives under $HOME/Develop/go.

We also need to setup a GOROOT variable for Go to work properly.

We will setup all of these in bash_profile. If you use other shell (like zsh), you will have to modify the respective config file.

Open bash_profile in your favorite editor—vim , code, cot, or plain text editor. Add these at the end of the file.

export GOPATH=$HOME/Develop/go
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

Ensure you change GOPATH to correct directory.

Save and exit. Make it valid for the current terminal session by issuing, source ~/.bash_profile.

4. Create Go workspace

Go places its files in three directories: All source code lives in src, package objects lives in pkg, and the compiled programs live in bin. Let us create them.

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

5. Create a hello world program

Now that we have installed and setup Go, let us write a hello world program. Open your favorite text editor and type these lines:

package main
import "fmt"

func main() {
    fmt.Printf("Hello world!\n")
}

Save it as hello.go.

Run the program with go run hello.go. It should output Hello World!.

Congrats. You are now ready to become a master in Go language.


Share this post on

Twitter | | |
Sign up for my weekly newsletter

I will send blog updates to this email. You can unsubscribe at any time using the link in those emails.