GO111MODULE = “off”
If GO111MODULE=”off” and the code is not in GOPATH/ SRC, then main.go is not in GOPATH/ SRC. That is, the following main.go is not in the GOPATH/ SRC directory, and I want to use content from another module that is not in the standard library or GOROOT (we don’t normally modify GOROOT).
An error occurs when running the code
main.go:4:2: cannot find package "module" in any of:
/usr/local/go/src/module (from $GOROOT)
/home/linux/go/src/module (from $GOPATH)
Copy the code
Solution set GOPATH
Go env -w GOPATH=~/test # Set this to ~/test because I need to write code under ~/test/ SRCCopy the code
Then write in the $GOPATH/ SRC directory
The go compiler looks for modules under $GOPATH/ SRC. Each directory under SRC can be a module, and a directory in a directory can be a module if we need to access modules in a directory within a directory, as shown in the following figure
We need to call the moduleA module in module, just use
import "module/moduleA"
Copy the code
GO111MODULE = “on”
With GO111MODULE=”on”, we call the written module directly, as shown below
An error is reported directly
main.go:4:2: package module is not in GOROOT (/usr/local/go/src/module)
Copy the code
The solution
- First: set GO111MODULE=”off” and then set GOPATH as above
- The second way: use the Go mod as follows
First we need to initialize a go.mod using
Go mod init test # Test can be any nameCopy the code
Then we import the module, starting with test(defined at initialization), followed by the module path, for example
import "test/module"
Copy the code
If Goland returns an error this way, but it compiles normally, delete the.idea directory in the current directory and restart the project
The same is true if we want to reference nested modules
import "test/module/moduleA"
Copy the code