0

I am currently migrating from GOPATH to go.mod. However, I cannot understand this:

  1. I read everywhere that GOPATH and go.mod does not mix, either this or that

  2. but then the go modules use GOPATH to download requirements...

So, the go modules approach uses GOPATH, which it claims to replace...

As I try to wrap my head around what is going on, my understanding is this:

GOPATH was originally a way to manage user projects AND dependencies. The user projects are now managed by the go module system, but the other functionality, the external/remote dependencies are still under GOPATH jurisdiction. GOPATH essentially became what a virtual env is in python, a library-dump.

Consequently, the go directory structure is something like this:

$HOME/
$HOME/programming
$HOME/programming/go
$HOME/programming/go/deps <- GOPATH points here
$HOME/programming/go/module1
$HOME/programming/go/module2

By setting GOPATH to different dirs in the build script, I could have different compile environments. However, as far as I understand, this is not needed, as go modules can handle if different projects in the same module require different versions of the same dependency.

$HOME/programming/go/deps1
$HOME/programming/go/deps1/some_lib.1.1
$HOME/programming/go/deps2
$HOME/programming/go/deps1/some_lib.2.2

Questions:

If GOPATH is the past since 1.11, why is it still fundamental at 1.19?

Is my approach to answer the above question correct, or do I misunderstand something?

Zoltan K.
  • 1,036
  • 9
  • 19
  • 4
    It’s just a convenient path for a default GOMODCACHE and GOBIN. If you cache modules and binaries elsewhere, then it’s not used at all. You don’t use it directly for any development. – JimB Jul 09 '23 at 14:59
  • 1
    @JimB Oh, I see, so `GOPATH` is not a primary setting anymore, but a fallback to some other values. – Zoltan K. Jul 09 '23 at 15:32

1 Answers1

-1

I set my GOPATH to ~/gp

ls ~/gp/pkg/mod/github.com/a
acomagu/         alecthomas/      andrewarrow/     apparentlymart/  aws/
agext/           alexkohler/      andreyvit/       armon/           aymanbagabas/
agnivade/        alingse/         andybalholm/     ashanbrown/      aymerick/
akamai/          aliyun/          anmitsu/         atotto/  

And everytime some import statements needs a package, it will end up there. In example above I just hit a[tab] to show a small section of all the stuff in that directory, there is a lot.

When inside a directory with a go.mod file I run:

go mod tidy
go build

The tidy command it great! It will download what is missing or use what you got.

Andrew Arrow
  • 4,248
  • 9
  • 53
  • 80