Carolyn Van Slyck

Building Go from Source

Gophers At Work

This is a part of a larger effort that I’m calling GopherSource, and our goal is for more of the gopher community to become upstream Go contributors.

Before we can become Go contributors, the very first step is being able to build Go from source. I followed the official doc and filled in the blanks a bit to figure out how to get everything working.

Go is compiled with … Go! 🀯 Since I already have Go 1.10 installed on my computer, I decided to use that. Another option is to bootstrap Go, Karen Carcamo wrote up how to do that on Linux.

Here is the tldr of how to download the Go source and build it:

  1. Clone the Go repository, it doesn’t have to be in your GOPATH.

     git clone https://go.googlesource.com/go
    

    If you want to build a specific version of Go, now’s the time to checkout that version using a tag, for example git checkout go1.10.3. Since we want to try out new features, namely Go Modules (the prototype formerly known as vgo), I am going to use the latest changes on the master branch. If you just cloned, you can skip this step since you already have the latest changes.

     git checkout master
     git pull
    
  2. Compile Go! If you are on Windows, use make.bat instead.

     $ cd src
     $ ./make.bash
     Building Go cmd/dist using /usr/local/Cellar/go/1.10.3/libexec.
     Building Go toolchain1 using /usr/local/Cellar/go/1.10.3/libexec.
     Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
     Building Go toolchain2 using go_bootstrap and Go toolchain1.
     Building Go toolchain3 using go_bootstrap and Go toolchain2.
     Building packages and commands for darwin/amd64.
     ---
     Installed Go for darwin/amd64 in /Users/carolynvs/src/go
     Installed commands in /Users/carolynvs/src/go/bin
    
  3. Open a new shell session. Now prepend the bin directory from the output above to your PATH so that you are using your custom go binary by default.

     export PATH=/Users/carolynvs/src/go/bin:$PATH
    

    On Windows use this command instead:

     $env:PATH = "/Users/carolynvs/src/go/bin;"+$env:PATH
    

    Make sure to replace /Users/carolynvs/src/go with the location where you cloned the Go repository.

  4. Use the version command to verify that you are now using your custom build of Go.

     $ go version
     go version devel +a12c1f26e4 Tue Jun 26 20:00:51 2018 +0000 darwin/amd64
    

Excited Dog: I DID A GO!

Now that we have the latest version of Go, let’s try it out and use a new feature that has been added to Go 1.11, Go Modules! Go Modules is the official name going forward for the vgo prototype and is the next step in the evolution of dependency management in Go.

Luckily, you don’t need to know what that is in order to see it in action. Now when you run go build, Go will take care of retrieving your dependencies, not just master but even a specific version or commit! πŸŽ‰

  1. Clone my test repository, ANYWHERE YOU LIKE! Doesn’t have to be in the GOPATH.

     git clone https://github.com/carolynvs/go-depmgmt-testrepo
    
  2. My repo relies upon external code that isn’t checked into the vendor directory. When you run the code, Go retrieves v0.8.0 of the errors package before building.

     $ go run main.go
     go: finding github.com/pkg/errors v0.8.0
     go: downloading github.com/pkg/errors v0.8.0
     hello world!
    

    There are some extra debug lines printed in the output, those are from the new Go modules feature:

     go: finding github.com/pkg/errors v0.8.0
     go: downloading github.com/pkg/errors v0.8.0
    
  3. Try running the code again, and you won’t see those extra lines.

     $ go run main.go
     hello world!
    

    Go downloaded the dependency and cached it in $GOPATH/src/mod. Next time you build, Go will retrieve that package from its cache.

     $ tree ~/go/src/mod
     ~/go/src/mod
     β”œβ”€β”€ cache
     β”‚Β Β  β”œβ”€β”€ download
     β”‚Β Β  β”‚Β Β  └── github.com
     β”‚Β Β  β”‚Β Β      └── pkg
     β”‚Β Β  β”‚Β Β          └── errors
     β”‚Β Β  β”‚Β Β              └── @v
     β”‚Β Β  β”‚Β Β                  β”œβ”€β”€ list
     β”‚Β Β  β”‚Β Β                  β”œβ”€β”€ v0.8.0.info
     β”‚Β Β  β”‚Β Β                  β”œβ”€β”€ v0.8.0.mod
     β”‚Β Β  β”‚Β Β                  β”œβ”€β”€ v0.8.0.zip
     β”‚Β Β  β”‚Β Β                  └── v0.8.0.ziphash
     └── github.com
         └── pkg
             └── errors@v0.8.0
                 β”œβ”€β”€ LICENSE
                 β”œβ”€β”€ README.md
                 β”œβ”€β”€ appveyor.yml
                 β”œβ”€β”€ bench_test.go
                 β”œβ”€β”€ errors.go
                 β”œβ”€β”€ errors_test.go
                 β”œβ”€β”€ example_test.go
                 β”œβ”€β”€ format_test.go
                 β”œβ”€β”€ stack.go
                 └── stack_test.go
    

    We will go into more detail about Go modules and what’s happening under the covers in the next blog post!

Gopher hanging on GO balloons

πŸš€ Step 1 of becoming a real Go contributor accomplished! πŸš€

If you’d like to play with Go Modules more, a bunch of friendly gophers are sharing what we learn as we go along and you can join us in the Go forums in the Exploring Go Modules thread. Otherwise if you want all the info but don’t have time to play, I will do a followup post in a few days summarizing what we discover.