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:

  1. 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.
  2. 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:

  1. 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 support go.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.

vgo is a package management tool based on the Go Module specification, similar to the official go mod command tool.

  1. Set Goland to enable vgo

Go Module - 图1

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.

  1. Manually modify the go.mod file as follows:
  1. module my-hello
  2. 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.

Go Module - 图2

  1. Subsequently, the go.mod file is automatically updated to:
  1. module my-hello
  2. require github.com/gogf/gf v1.6.13

Where v1.6.13 represents the latest framework version detected by vgo.

  1. If the following situation occurs after downloading the latest code framework: https://www.jetbrains.com/help/go/create-a-project-with-vgo-integration.html

Go Module - 图3

  1. Press the shortcut ⌥(option)+↩(return) or right-click and choose Sync packages of my-hello

Go Module - 图4

  1. After waiting a few seconds, you can see the left Go Module with content, and the terminal automatically outputs the downloaded framework version.

Go Module - 图5

Using the Command Line

  1. Open Terminal and execute in the project root directory:
  1. 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

Go Module - 图6

  1. Subsequently, the go.mod file content is automatically updated to:
  1. module my-hello
  2. 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.