DEV Community

Cover image for Setup Golang Locally on Mac OSX
Alabo David Briggs
Alabo David Briggs

Posted on • Originally published at alabobriggs.hashnode.dev

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 
Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode

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

Step 1 - Click on the link as shown below

Alt Text

Step 2 - Click on the downloaded package

Alt Text.

Step 3 - Follow installation steps and complete the installation

Alt Text

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
Enter fullscreen mode Exit fullscreen mode

you should get this output

go version go1.14 darwin/amd64
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

A new interface on the terminal that looks like this will be shown.
NOTE: Yours will most likely be empty.
Alt Text

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

export GOPATH=$HOME/go
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
Enter fullscreen mode Exit fullscreen mode

Alt Text

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
Enter fullscreen mode Exit fullscreen mode

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

echo ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

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;
Alt Text

To test that everything works perfectly run

go get github.com/digitalocean/godo
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

type in the text file

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
Enter fullscreen mode Exit fullscreen mode

Then control + o then return to save and control + x to return.

To test this type this command in your terminal

go hello.go
Enter fullscreen mode Exit fullscreen mode

OUtput

Hello, World
Enter fullscreen mode Exit fullscreen mode

You can also install your new package by typing

go install hello.go
Enter fullscreen mode Exit fullscreen mode

and also build;

go build hello.go

Enter fullscreen mode Exit fullscreen mode

.
.
.
.

Conclusion

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

Top comments (0)