Sorting issues in Go modules and imports

E. Roberts
3 min readAug 10, 2020

(My latest bake! A classic Victoria sponge: vanilla buttercream with raspberry jam. It was delicious.)

So I wanted to do a bit of go development on my home computer. I hadn’t done much development on the laptop since I bought it probably six months ago but I assumed it would be in good shape. I was wrong. Go 1.10 had been installed in January, presumably using apt and I was getting “is a GOROOT, not a GOPATH” errors all over the place. Given 1.10 is from 2018 and go modules have happened since then updating seemed like a bit of a no brainer. This time I updated using the instructions in the go docs (https://golang.org/doc/install).

Things still didn’t work. I was still seeing lots of “is a GOROOT, not a GOPATH” errors. I’d installed the go package to /usr/local/go as go expects by default, so I suspected I’d set GOROOT to something silly at some point, and I also couldn’t quite remember what the difference between GOROOT and GOPATH was, so I looked that up.

GOROOT is the place where the main go package and go binary are installed. By default it is /usr/local/go. If you choose to install somewhere else, you’ll need to change GOROOT to be that, in whichever way your shell sets environment variables.

GOPATH is where go looks for packages. If you’ve got a normal setup, this should be set to something like $HOME/go.

Because it came up in my travels and I found it interesting: GOBIN is where go install and go get will place binaries, $GOPATH/bin by default. I found this info here: https://golang.org/doc/code.html.

So after reading that I set GOROOT to /usr/local/go and GOPATH to $HOME/go (for anyone trying to debug similar situations, running `which go` gave me /usr/local/go/bin/go), where the go code I’d been working on was stored in $HOME/go/src/<my-repo-name>.

That got `go get` working for me again, which made me pretty happy. However, I then tried to build my local code, which imports a package I’ve written. This gave me the error “package go/src/<my-repo-name>/<package> is not in GOROOT”, which felt a bit like the laptop wasn’t even trying. Of course, it’s not in GOROOT! I know that! Why is it even looking there?!

Fortunately before I burst a blood vessel, I remembered that go modules are a thing. I had the environment variable GO111MODULES set to on, so I needed to create a modules. I ran go mod init in my new repo and tada, my package built. (Well, it had compiler errors, but same difference.)

Definitely took a lot longer than I was expecting for me to be able to run my code, but glad I understand more about Go now!

Incidentally, this is a great read on what gomodules are and why they’re useful: https://blog.golang.org/using-go-modules

--

--

E. Roberts

Software engineer, passionate about SRE and building maintainable systems efficiently