Go Module
is an official package management tool provided since Go version 1.11.1
, used for package management and dependencies in Go projects, similar to PHP's composer
and Node.js's npm
. This chapter introduces some commonly used commands/settings for Go Module
. For more detailed information, please refer to the official documentation: https://github.com/golang/go/wiki/Modules
About go.mod
go.mod
is the dependency description file for a Go project. The file primarily describes two things:
- What is the current project name (
module
). Each project should set a name, and packages in the current project can use this name to call each other. - The names of third-party packages that the current project depends on. When the project runs, it will automatically analyze the code dependencies in the project, generate a
go.sum
dependency analysis result, and then the Go compiler will download these third-party packages before compiling and running.
We've made some changes to the previous hello world
project, adding a go.mod
file (which can also be generated by using the go mod init project_name
command in the project's root directory) with the following content:
module my-hello
Here, my-hello
is the name of the current project and can be set arbitrarily.
Thus, the project's module
initialization is completed simply.
Generally, the go.sum
dependency analysis file should be added to version control and committed along with the go.mod
file.
Using go.mod
Using go.mod
means managing project dependencies with go.mod
. We have two ways to use go.mod
: IDE-vgo
and command line
. Below, we demonstrate how to use these two methods to manage dependencies with the GoFrame
framework.
To have
Goland
IDE supportgo.mod
, vgo support (including code dependency detection) must be enabled. The difference between these two methods lies only in the way of downloading dependency packages.
Using Goland IDE vgo (recommended)
vgo
is a package management tool based on the Go Module
specification, similar to the official go mod
command tool.
- Set
Goland
to enablevgo
If your local environment already has VPN
functionality, you can ignore setting the proxy
.
Enter the proxy address to download dependency packages, or choose direct
to not use a proxy. Available reverse proxy addresses include:
https://goproxy.cn
https://goproxy.io
https://mirrors.aliyun.com/goproxy/
See the Go official website for more information: https://github.com/golang/go/wiki/Modules#are-there-always-on-module-repositories-and-enterprise-proxies
Make sure to select an input proxy address here.
- Manually modify the
go.mod
file as follows:
module my-hello
require github.com/gogf/gf latest
Adding the dependency on the GoFrame
framework, where latest
means using the latest version, the IDE will immediately update and download the framework code. Upon success, the IDE will modify the go.mod
file and generate a go.sum
dependency analysis file.
- Subsequently, the
go.mod
file is automatically updated to:
module my-hello
require github.com/gogf/gf v1.6.13
Where v1.6.13
represents the latest framework version detected by vgo.
- If the following situation occurs after downloading the latest code framework: https://www.jetbrains.com/help/go/create-a-project-with-vgo-integration.html
- Press the shortcut
⌥(option)+↩(return)
or right-click and chooseSync packages of my-hello
- After waiting a few seconds, you can see the left
Go Module
with content, and the terminal automatically outputs the downloaded framework version.
Using the Command Line
- Open
Terminal
and execute in the project root directory:
export GO111MODULE=on GOPROXY=https://goproxy.cn; go get -u github.com/gogf/gf
This command will immediately download the latest stable version of the GoFrame
framework. The export GO111MODULE=on;
indicates enabling the Go Module
feature (Go 1.11.x
versions are disabled by default and need to be manually enabled), and export GOPROXY=https://goproxy.cn
indicates using a proxy for download, for obvious reasons, and can also considerably increase the download speed of dependency packages. Other proxy addresses that can be used are:
https://goproxy.cn
https://goproxy.io
https://mirrors.aliyun.com/goproxy
- Subsequently, the
go.mod
file content is automatically updated to:
module my-hello
require github.com/gogf/gf v1.6.13 // indirect
And generates a new go.sum
dependency analysis file, which is essentially a temporary file and not very meaningful for our daily development work.