getting the go tour running locally

2020-04-23

 | 

~4 min read

 | 

614 words

In A Tour of Go, one of the options is to run the tour offline.

The instructions seem simple enough:

  1. Install Go

  2. Run the command to fetch the tour

    go get golang.org/x/tour
  3. Run the binary that’s placed in the workspace’s bin directory.

    First things first: workspaces seem to have been deprecated with the introduction of modules in v1.13+.1

    Why does any of this matter? Because when I followed the instructions:

    stephen@Stephens-MBP-2 ~ % go get golang.org/x/tour
    stephen@Stephens-MBP-2 ~ % $HOME/go/bin/tour

    The tour successfully downloaded, and I was able to run the binary… but, I couldn’t execute anything.

    Signal Killed

    Trying to run the Hello, world, I got the message: Program exited: signal: killed.

    Womp, womp.

Debugging The Issue

First, let’s look at a few things:

  1. The instructions to install Go (on Mac) indicate that by following the wizard, /usr/local/go/bin should be automatically available in the PATH. Checking that, it appears that’s true!

    $ echo $PATH
    /Users/stephen/.nvm/versions/node/v12.16.1/bin:/Users/stephen/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Users/stephen/.cargo/bin
  2. But when I saved the tour (with the go get), where did that save and install? Not in /usr/local/go, but /Users/stephen/go - this is because that’s my GOPATH value:

    $ go env
    ...
    GOBIN=""
    GOPATH="/Users/stephen/go"
    GOROOT="/usr/local/go"
    ...

    In my haste to get started, I didn’t fully comprehend Go’s How to Write Go Code, specifically, in describing the installation of a hello.go it reads:

    $ go install example.com/user/hello
    $

    This command builds the hello command, producing an executable binary. It then installs that binary as $HOME/go/bin/hello (or, under Windows, %USERPROFILE%\go\bin\hello.exe). The install directory is controlled by the GOPATH and GOBIN environment variables . If GOBIN is set, binaries are installed to that directory. If GOPATH is set, binaries are installed to the bin subdirectory of the first directory in the GOPATH list. Otherwise, binaries are installed to the bin subdirectory of the default GOPATH ($HOME/go or %USERPROFILE%\go).

    What’s all this mean? Well, it means that by default, the Go binaries will be saved in /Users/stephen/go/bin, (which is also often written as $HOME/go/bin or ~/go/bin).

Side Note: Simpler Go Execution

We can make executing Go binaries a much simpler process then by adding this to our PATH, like so:

.zshrc
...
export PATH="$PATH:$HOME/go/bin" # This adds User Installed Go Binaries to PATH

Okay, so that makes it simpler to run the tour, which can now be done as:

go get golang.org/x/tour
tour

Finding A Solution

None of these hold the answer, yet, however.

Even though it’s now easier to run the tour, it still doesn’t actually execute the Go code.

To get it working, however, manually building and installing does seem to work:

go get -d golang.org/x/tour // Not necessary if the source code is already in `$HOME/go/src`
cd $HOME/go/src/golang.org/x/tour
go build
go install
tour

Credit to Michael DuBose for this workaround, which I found on this Github issue.

Regarding the -d flag for go get:

The -d flag instructs get to download the source code needed to build the named packages, including downloading necessary dependencies, but not to build and install them.

With that being the case, manually building and installing is necessary, but it works!

Success

And now, I can get to actually learning Go!

Footnotes



Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!