Error: "Cannot find package" Despite Proper GOPATH Setting
When attempting to build Go packages using "go build" or "go run," you may encounter an error message "cannot find package" even though GOPATH is correctly set.
Explanation
The issue arises because Go's build system expects packages to reside in directories that match their package names. In the provided example:
foobar.go
...the package name is "foobar." However, the code is located in a directory without that name.
Solution
To resolve the issue, move the source file to a directory that matches the package name:
Recommended Additional Steps
For convenience and best practices:
PATH="$GOPATH/bin:$PATH"
The above is the detailed content of Why Can't Go Find My Package Despite a Correct GOPATH Setting?. For more information, please follow other related articles on the PHP Chinese website!