πŸ“Œ What is GOPATH?

In early versions of Go (before Go modules were introduced in Go 1.11 / made default in Go 1.16), the GOPATH environment variable defined the workspace directory where all your Go code, dependencies, and binaries lived.

It typically looked like this:

$GOPATH/
 β”œβ”€β”€ src/       # source code for your projects and dependencies
 β”œβ”€β”€ pkg/       # compiled package objects
 └── bin/       # compiled executables (from `go install`)

For example:

This structure was mandatory back then β€” you couldn’t just put your project anywhere.


πŸ“Œ Why was it needed?

Because Go needed a deterministic way to:

Without modules, the GOPATH was essentially the global "workspace root."


πŸ“Œ What replaced it?

Since Go modules (go mod init, go.mod) became the default in Go 1.16 (2021), you no longer need to put code inside GOPATH.

With modules: