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:
Install Go
Run the command to fetch the tour
go get golang.org/x/tour
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.
Trying to run the Hello, world
, I got the message: Program exited: signal: killed
.
Womp, womp.
First, let’s look at a few things:
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
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 theGOPATH
andGOBIN
environment variables . IfGOBIN
is set, binaries are installed to that directory. IfGOPATH
is set, binaries are installed to thebin
subdirectory of the first directory in theGOPATH
list. Otherwise, binaries are installed to the bin subdirectory of the defaultGOPATH
($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
).
We can make executing Go binaries a much simpler process then by adding this to our PATH, like so:
...
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
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!
And now, I can get to actually learning Go!
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!