Installing Go Lang on Mac (Homebrew)
I’m starting to learn GoLang today and they’ve a coding playground on the homepage which is really cool and easy to get started but I’d still like to install it on my machine for developing complex apps and I think you too are looking for the same :P
I’m on macOs Mojave(10.14.5) and Homebrew version 2.2.16 but it shouldn’t be a problem with a different version.
Installing GO
$ brew install golang
Verify the installation
$ go version
In future to update GO first update Brew and then Go. It’s not required now since we just installed it.
$ brew update && brew upgrade golang
Creating workspace
It’s considered best practice to use $HOME/go
location for your workspace, so let’s do that!
$ mkdir -p $HOME/go/{bin,src}
Environment setup
Some third party tools still depend on $GOPATH
variable being set. You can set your $GOPATH
by adding it to your .zshrc
or .bashrc
$ export GOPATH=$HOME/go
$ export PATH=$PATH:$GOPATH/bin
reload the settings with source $HOME/.zshrc
(or .bashrc
) or quit and start a new terminal
To start a new project make a new folder in src. (~/go/src/hello_world/
)
Testing the installation
We’ll test wether Go is installed correctly by building a simple program, as follows.
Create a file named hello.go
by:
$ vim hello.go
that looks like:
package mainimport "fmt"func main() {
fmt.Println("hello, world!")
}
Exit vim by typing the ESC
followed by :wq
and hit enter
. Run it with the go tool.
$ go run hello.go
If you see the “hello, world” message then your Go installation is working.
Now I’ll start reading the How to Write Go Code document, which describes some essential concepts about using the Go tools.
‘GO have a look xD’
BONUS:
Testing the installation
Go 1.11 introduced Modules, enabling an alternative workflow. This new approach will gradually become the default mode, deprecating the use of GOPATH
.
Modules aim to solve problems related to dependency management, version selection and reproducible builds; they also enable users to run Go code outside of GOPATH
.
Select any directory outside GOPATH
as the root of your project, and create a new module with the go mod init
command.
A go.mod
file will be generated, containing the module path, a Go version, and its dependency requirements, which are the other modules needed for a successful build.
If no <modulepath>
is specified, go mod init
will try to guess the module path from the directory structure, but it can also be overridden, by supplying an argument.