The following tutorial column will introduce to you why it is no longer recommended to use GOPATH and how to use Go Modules. I hope it will be helpful to friends in need! Reason: With Go Modules
History:
An environment variable used to tell GO when compiling the project Where to read source files
Restriction rules:
Requires the project to be written under GOPATH
- Needs to create a directory based on the actual URL path of the project. For example, github.com/foo/bar => $GOPATH/src/github.com/foo/bar
- There is no built-in version tracking for extension packages
-
-
With Go Modules
Only affects the development environment, not the production environment
- Compatible with GOPATH, you can still use GOPATH
-
- to Go Modules
version requirements>=1.11## according to your personal preferences.
-
#go mod init
Command initialization, such as:
mkdir myProject/
cd myProject
go mod init github.com/myGithubName/myProject
Copy after login
GOPATH project migration, such as:
mv ~/go/src/github.com/myGithubName/myProject ~/anywhere_else/
cd ~/anywhere_else/myProject
go mod init github.com/myGithubName/myProject
Copy after login
The above content is compiled from: https://medium.com/@vingarcia00/golang-why-not-use-the-gopath-87521259663a
In addition, if you use Goland IDE, open Settings and go to Go -> Go Modules (vgo), check Enable Go Modules (vgo) Integration, otherwise the extension package will not be recognized.
go language column!
The above is the detailed content of Do you know why using GOPATH is no longer recommended and how to use Go Modules. For more information, please follow other related articles on the PHP Chinese website!