1

A section of the Go Modules Reference describes some rules for minimal module compatibility. There is a list of conditions in this section and two of them look like this:

  • No $GOPATH[i]/src/$modpath/$vn/$suffix directory exists (for any root $GOPATH[i]),
  • The file $GOPATH[d]/src/$modpath/go.mod exists (for some root $GOPATH[d]) and declares the module path as $modpath/$vn,

In this context, what is $GOPATH[i] and $GOPATH[d]? (Assume we know what $GOPATH is.)

Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158

1 Answers1

2

As stated on the documentation for the go command:

The GOPATH environment variable lists places to look for Go code. On Unix, the value is a colon-separated string. On Windows, the value is a semicolon-separated string. On Plan 9, the value is a list.

If the environment variable is unset, GOPATH defaults to a subdirectory named "go" in the user's home directory ($HOME/go on Unix, %USERPROFILE%\go on Windows), unless that directory holds a Go distribution. Run "go env GOPATH" to see the current GOPATH.

(source)

Thus, the syntax you references $GOPATH[i] is simply used to describe an item in the GOPATH, which as we've seen can be a list and not just a reference to a single path.

Let's assume I have the following GOPATH on my Linux machine: /home/me/go:/gofaster, and I'm working on a project in /home/me/go/src/example.com/testing.

The minimal module compatibility rules would say that in order ie. for my dependency example.com/utils/v2 to be resolved, Go would check:

  • that I don't have a package in /home/me/go/src/example.com/testing/vendor/example.com/utils/v2 (ie. vendor directory)
  • that I don't have a package in none of /home/me/go/src/example.com/utils/v2, nor in /gofaster/src/example.com/utils/v2 (ie. checking BOTH gopaths).
  • that I have a package in one of /home/me/go/src/example.com/utils or /gofaster/src/example.com/utils (notice the lack of v2).

If I do have a package in either of those directories, then it is used to resolve the dependency.

morganbaz
  • 2,997
  • 1
  • 17
  • 30