Setting up Go 1.22 on macOS with Homebrew: `go: go.mod file not found` and GOPATH confusion
I came back to Go after a couple of years away and the first thing that bit me was that every go build failed. I'd installed Go 1.22 with Homebrew on macOS 14, which I assumed would just work. It did, in the sense that go version printed the right number. The complaints started when I tried to build a checkout of an old project.
## The error
$ go build
go: go.mod file not found in current directory or any parent directory; see 'go help modules'$ go version
go version go1.22.2 darwin/arm64
I was confused because I remembered Go projects just building from anywhere inside your GOPATH. That memory is the problem, it's about ten years out of date.
## What changed
Since Go 1.11 modules have been the default way to manage dependencies, and since 1.16 GO111MODULE=on is the default. A go.mod file is now the root marker of a module. Outside one, the go command doesn't know what module path you're on or which dependency versions to use, so it refuses to guess. My old checkout predated modules and had no go.mod, which is why it fell over immediately.
## Getting a project to build
For a new project, initialize the module at the directory root:
mkdir hello && cd hello
go mod init example.com/hello
go build
That writes a two-line go.mod and everything builds. For an existing GOPATH-era project, the migration command does most of the lifting:
go mod init github.com/someone/legacy-project
go mod tidy
go mod tidy resolves imports and pins versions. It can take a while and it will complain about some old dependencies, but it beats doing it by hand.
## The GOPATH part nobody told me
I'd set GOPATH in my .zshrc from an old tutorial. With modules on, GOPATH is mostly irrelevant for builds; it's just where the module cache lives. I confirmed mine:
$ go env GOPATH GOMODCACHE GO111MODULE
/Users/me/go
/Users/me/go/pkg/mod
on
Homebrew had installed Go to /opt/homebrew/bin/go and it was first on PATH, so the install itself was fine. The real fix was stopping treating GOPATH as the project root and putting go.mod where it belonged.
While I was in there I cleaned up the stale env var with go env -w GO111MODULE= to put it back to auto, though on 1.22 it doesn't matter much since modules are always on. The thing that finally made it click was go env GOMOD, which prints the path to the active go.mod. Running it from the project root gave me the file; running it from anywhere else printed /dev/null. That one command tells you instantly whether you're inside a module, which is more than I can say for the error message.
## One more gotcha
I kept getting a different error in a subdirectory until I realized go mod init has to run at the top level, not in the folder I happened to be in:
go: cannot find main module, but found .git/config in /Users/me/src/legacy
to create a module there, run:
go mod init
Running it one directory up fixed that. If you've got an opinion on whether old projects are worth migrating at all versus pinning a pre-module Go toolchain, I'd genuinely like to hear it.