Git

Golang Environment Configuration

I know that in my previous post, mocking API's in Golang, I said I would talk about testing, but I lied. To your face.


I know that in my previous post, mocking API's in Golang, I said I would talk about testing, but I lied. To your face. I'm actually going to take a step backwards and talk a bit about the Golang environment configuration.

Back when I first started to tinker with Go, I struggled getting the environment set up. Once I did get it working and was finally able to compile hello_world.go, I just accepted Go's environment as some sort of secret voodoo. This seems to be a recurring pattern, as I've encountered a few developers who have had similar experiences. And in fact, the reason I'm writing this post instead of one about testing is because a colleague approached me and asked if I could do a post about setting Go up. So here it is. You're welcome, friendo.

Environment Variables

There are several variables that may or may not need to be configured based on how you install Go. This is an incomplete list, check the golang docs for more.

  • GOROOT
  • GOBIN
  • GOOS
  • GOARCH
  • GOPATH

GOROOT is the path to where the Go standard library is located on your local filesystem. Go assumes a default location of /usr/local/go, so do NOT set this if that's where you've extracted Go. If you want to install Go somewhere besides /usr/local/go, then be sure to set the $GOROOT to whichever path you chose.

GOBIN is the path to where your Go binaries are installed from running go install. Only set this if you want to change it from the default location $GOPATH/bin.

GOOS is my personal favorite and specifies the operating system. Optional to define.

GOARCH is used to specify the processor architecture. Also optional.

GOPATH is the path to your workspace. It is required to be set and has no default. I usually make my $GOPATH something like /home/matt/code/go

GOPATH Structure

Once $GOPATH is defined (don't forget to add export GOPATH="/path/to/your/workspace" to your .bashrc) we need to make sure the directory $GOPATH points to has the proper structure. Go expects 3 sub-directories:

    bin/
    pkg/
    src/

bin/ contains executables which are generated by running a go install

pkg/ contains installed package objects. Running a go get will place package objects here.

src/ is --SURPRISE-- where our source code will live.

The src/ directory will contain all of our projects and should have the following structure:
Source-Control-Platform/User/Repository

As an example let's say that I have a Go project on github, named "BestApp". The src/ directory would look like:
github.com/MattBorowiec/BestApp

Walkthrough

  1. Download the appropriate archive for your platform.

  2. Extract the archive.

    tar -C /usr/local -xzf go1.X.X.platform-arch.tar.gz

    NOTE: as mentioned earlier, if extracted to /usr/local, then GOROOT does not need to be explicitly set.

  3. Add Go Binaries to your PATH.

    export PATH="$PATH:/usr/local/go/bin"

  4. Verify you have access to GO binaries.

    Running go in your shell should now print out a list of Go commands.

  5. Set your GOPATH

    export GOPATH="/home/matt/code/go"

  6. Add a src/ directory inside your GOPATH

    mkdir $GOPATH/src

  7. Add namespace to the src/ directory

    cd $GOPATH/src<br/> mkdir github.com/MattBorowiec/BestApp

    NOTE: Remember to use the source-control-platform/user/repo structure

  8. Add a go source code file to your namespace.

    cd $GOPATH/src/github.com/MattBorowiec/BestApp/

    touch best.go

  9. Add some source code to the file

          
            package main
     
            import "fmt"
     
            func main() {
                fmt.Println("YOU ARE THE BEST!")
            }
     
  10. Save the file, run the code, and verify the output

    [matt@localhost BestApp]$ go run best.go

            YOU ARE THE BEST!
     

If YOU ARE THE BEST! then we now have a fully functioning Go environment.

Now that we have a Go environment setup, in my next post I'll dive a bit deeper into mocking APIs and add some tests.

Similar posts

Get notified on new marketing insights

Be the first to know about new B2B SaaS Marketing insights to build or refine your marketing function with the tools and knowledge of today’s industry.