Home > Article > Backend Development > Introduction to go language dependency management
The Go language is an open source programming language that makes it easy to construct simple, reliable and efficient software. go language tutorial column will introduce you to go language dependency management.
go mod instructions
Add dependencies
if we need For a third-party framework, first check its URL address on github. For example, the gin framework is github.com/gin-gonic/gin
. First turn on the module mode, that is, SET GO111MODULE = on. The default is auto. (Be careful to always execute the command in the current project directory to avoid any mistakes)
SET GOPROXY=https://goproxy.cn Set the proxy to domestic, otherwise the download will not work, add env -w GOPROXY=https://goproxy.cn,direct (In goland, it can be modified directly through settings. There is no need to execute this command every time. The same is true for turning on module mode earlier)
Use the go mod init command to generate The go.mod file of the current project
If the required framework does not exist locally (under GOPATH), execute go get -u github.com/gin-gonic/gin
, and you are done. There should be three copies of the framework, which are placed in pkg, GOPATH and External Libraries at the same level as src (actually it does not exist, goland virtualizes this directory, which includes the module directory of the current project and the src directory under GOROOT, that is, the SDK) directory, the editor identifies the code through the package under External Libraries), of course you can also use go mod download
, but this seems to have no parameters, so you should first add the framework in the go.mod file , to succeed, so we still use the previous one
If the required framework already exists locally, execute go install github.com/gin-gonic/gin
or in the go.mod file Add github.com/gin-gonic/gin
to require (it doesn’t seem to be possible to change the file directly, you should use go mod edit), or import github.com/gin-gonic/ in the program gin
Then execute go mod tidy
Delete dependencies
If this framework is no longer needed, executego mod tidy
, if there is no code related to the framework in the program, this instruction will automatically delete the framework in the go.mod
file
At this time, the framework in the External Libraries directory is deleted, and the remaining two places are retained. The result is that the editor cannot recognize the frame, so it cannot give code prompts, and will report an error in the code involving the frame.
Other instructions
go mod vendor is used to package all the dependencies of the current project into the vendor directory under the project (this directory is created by this command, and creating a new folder by yourself is invalid), so that the project and its dependencies can be easily Copy to another computer.
In addition to something else, after goland 2019 version 3.0, when it detects github.com/gin-gonic/gin, etc. in the clipboard, it will automatically generate a pop-up window asking whether to install, import, etc., if it is already After setting up the module mode and modifying the proxy, you can directly click the option on the pop-up window to operate
Currently vscode does not support the module mode well (the code prompt of the external framework cannot be given, or sometimes it is not available, It is not possible to automatically import packages), and the error display delay is too serious.
For more go language knowledge, please pay attention to the go language tutorial column on the php Chinese website.
The above is the detailed content of Introduction to go language dependency management. For more information, please follow other related articles on the PHP Chinese website!