Setup Golang Locally on Mac OSX

Setup Golang Locally on Mac OSX

Recently I switch from a linux development environment to OSX and one of the challenges I faced was setting up golang on OSX. This article will give you a run down on how to download, setup and get your first golang application up and running on osx.

Step 1 - Installing Golang

The first step to using golang is to have it installed on your computer. To confirm if a version of golang is not installed on your system, type in this command in your desired terminal, I use hyper but any will do.

go

you should see and output that looks like this if you have it installed

go is a tool for managing Go source code.

Usage:
        go <command> [arguments]
The commands are:

        bug         start a bug report
        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing sou
rce
        get         add dependencies to current module 
and install them
        install     compile and install packages and de
pendencies
        list        list packages or modules
        mod         module maintenance
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

        buildmode   build modes
        c           calling between Go and C
        cache       build and test caching
        environment environment variables
        filetype    file types
        go.mod      the go.mod file
        gopath      GOPATH environment variable
        gopath-get  legacy GOPATH go get
        goproxy     module proxy protocol
        importpath  import path syntax
        modules     modules, module versions, and more
        module-get  module-aware go get
        module-auth module authentication using go.sum
        module-private module configuration for non-public modules
        packages    package lists and patterns
        testflag    testing flags
        testfunc    testing functions

Use "go help <topic>" for more information about that topic.

If you do not get this output visit golang download page and follow the steps below to get golang installed.

Screenshot 2020-03-16 at 12.04.00 PM.png

Step 2 - Click on the downloaded package

Screenshot 2020-03-16 at 12.29.31 PM.png

Step 3 - Follow installation steps and complete the installation

Screenshot 2020-03-16 at 12.36.54 PM.png

Hurray. You have successfully completed the installation of golang on you mac.
To confirm that golang was installed correctly type the below command in your terminal

 go version

you should get this output

go version go1.14 darwin/amd64

Having successfully installed mac os let us move to setting it up on mac.

Step 2 - Setup your workspace

In order to start using go on osx you need to set your environmental variables, this will be used by golang to determines where to find and install new packages.

open .bash_profile file by typing the blow command in your terminal

 nano ~/.bash.profile

A new interface on the terminal that looks like this will be shown. NOTE: Yours will most likely be empty. Screenshot 2020-03-16 at 1.03.11 PM.png

Set you GOPATH variable to the location of your go folder by typing

export GOPATH=$HOME/go

When Go compiles and installs tools, it will put them in the $GOPATH/bin directory. It is common to add the workspace’s /bin subdirectory to your PATH in your ~/.bash_profile:

export PATH=$PATH:$GOPATH/bin

After these have been added your ~/.bash_profile file should look like this

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

Screenshot 2020-03-16 at 1.03.11 PM.png

When done press control + 0 to save the changes then return then control + x.

To update shell of the new changes type in the below command in your terminal

. ~/.bash_profile

And that will be all to confirm that your changes are saved;

echo ~/.bash_profile

and you should see your variables set.

Step 3 - Create first project

When go is installed an auto folder is created on your root directory named go. This contains your bin, pkg and src folders;

it is located in your root directory;

Screenshot 2020-03-16 at 1.28.24 PM.png

To test that everything works perfectly run

go get github.com/digitalocean/godo

You should the project in the src directory under the go directory

Step 4 - Create first go program

Open your terminal in your src/github.com/username/hello-world NOTE: Your projects should be structured in that form to aid go get to run smoothly. Github.com can be replaced with a link to where your project is.

Run the following command to create a new file

nano hello.go

type in the text file

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Then control + o then return to save and control + x to return.
To test this type this command in your terminal

go hello.go

OUtput

Hello, World

You can also install your new package by typing

go install hello.go

and also build;

go build hello.go

. . . .

Conclusion

Congratulations. You have successfully setup your go workspace on mac OSX