Prerequisutes
go
supports a wide range of operating systems. There are two ways go
can be installed on the system.
- Binaries
- Building it from the source
I am installing it on MacBook Pro with macOS Mojave v10.14.4 for this tutorial using the Mac binaries.
You could get the download link here
As shown in the image above, select the relevant package accoridng to the operating system. I have downloaded the one for Apple macOS
.
- Download file
Installation
The installation is a fairly straight. You could go with the defaults as shown below.
Verification
To verify if the go
has been successfully, it can be verified from the command line
You could use the following commands in the terminal to verfiy.
>>> which go # Returns path where go is installed/usr/local/go/bin/go>>> go version # Returns the version of the go installedgo version go1.12.4 darwin/amd64
If the terminal shows up as the above, then go
lang has been successfully installed.
Setting up workspace
Go
follows a different approach to manage the code which requires to setup the project workspace. This means all the go
projects should be developed and maintained in the defined workspace.
Steps to setup the workspace
Open the shell config which is locate in the HOME
directory.
NOTE:
-
bash_profile
or.bashrc
for BASH
- .zshrc
for Oh my Zsh!
Add the following variables to shell config
>>> export GOPATH=$HOME/<go-workspace> # <go-workspace> is a filler. Fill it with proper path>>> export PATH=$PATH:$GOPATH/bin>>> export GOROOT=/usr/local/opt/go/libexec>>> export PATH=$PATH:$GOROOT/bin
-
$GOPATH/src
: Go Projects source code -
$GOPATH/pkg
: Contains imported packages -
$GOPATH/bin
: The compiled binaries home
This completes the setup of go
. And system is ready for some code.
Top comments (2)
Nice article, Krishna! Some things to consider, if you are running the latest version of Go,
$GOPATH
is not necessary since we haveGOMODULES
! Yaaay π₯³π however,GOMODULES
is not enabled by default, you need to set your environment variable toexport GO111MODULE=on
See this blog post for more information blog.golang.org/using-go-modules.Thanks for pointing out, Richard. I'll update the post shortly. π