gogo-modulesgopath

What is $GOPATH[i] and $GOPATH[d]?


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:

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


Solution

  • 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:

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