---
title: Golang Environment Configuration
description: 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.
---

[Blog ](https://www.metaltoad.com/blog)

# [Golang Environment Configuration](https://www.metaltoad.com/blog/golang-environment-configuration)

 Written by [Metal Toad Staff](https://www.metaltoad.com/blog/author/metal-toad-staff) | Aug 15, 2016 12:00:00 AM

I know that in my previous post, [mocking API's in Golang](https://www.metaltoad.com/blog/mock-api-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](https://golang.org/), 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](https://golang.org/doc) 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](https://golang.org/dl/) 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.

[View full post](https://www.metaltoad.com/blog/golang-environment-configuration)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Metal Toad Staff"
  },
  "dateModified" : "2023-12-06T23:33:53.346Z",
  "datePublished" : "2016-08-15T00:00:00Z",
  "headline" : "Golang Environment Configuration",
  "image" : {
    "@type" : "ImageObject",
    "height" : 500,
    "url" : "https://6104926.fs1.hubspotusercontent-na1.net/hubfs/6104926/Imported_Blog_Media/golang.jpg",
    "width" : 870
  },
  "mainEntityOfPage" : "https://www.metaltoad.com/blog/golang-environment-configuration",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60.0,
      "url" : "https://6104926.fs1.hubspotusercontent-na1.net/hubfs/6104926/social-suggested-images/6104926.fs1.hubspotusercontent-na1.nethub6104926hubfsMetal_Toad_Logo_orange_horizontal-50-1.jpg",
      "width" : 240.00002
    },
    "name" : "Blog"
  }
}
```