Go modules detailed use


Reprint of the original textGo modules detailed use


sinceGoSince the official launch of 1.11 last year, a new dependency management module has been added and it is easier to manage the modules required in the project. A module is a collection of go packages stored in the file tree, and its root directory contains the go.mod file. The go.mod file defines the module path of the module, which is also the import path for the root directory, and its dependency requirements. Each dependency requirement is written as a module path and a specific semantic version.

Starting from go 1.11, go allows you to create projects using go.mod in any directory other than $gopath / SRC. In $gopath / SRC, the go command still runs in the old gopath mode for compatibility. Starting from go 1.13, the module mode will become the default mode.

This article will introduce a series of common operations when developing go code using modules:

  • Create a new module.
  • Add dependencies.
  • Upgrade dependencies.
  • Delete unused dependencies.

The following cases are based onGINModule as an example.
Before that, you need to set some environment variables:

export GO111MODULE=on
export GOPROXY= https://goproxy.io  //Set agent

Create a new module

You can create a new directory anywhere except $gopath / SRC. For example:

mkdir backend && cd backend

Then initializego mod init backend, after success, you will find that one will be generated in the directorygo.modDocuments

$ cat go.mod

The contents are as follows

module backend

go 1.12

Add dependency

Create a file main.go, and then add the following code. Here, the dependency package of gin is directly imported.

vim main.go

package main

import "github.com/gin-gonic/gin"

func main() {
    r := gin.Default()
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
    r.Run() // listen and serve on

After go build, the required dependent packages will be introduced in go.mod. Then let’s take a look at the go. Mod file.

module backend

go 1.12

require (
    github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 // indirect
    github.com/gin-gonic/gin v1.3.0
    github.com/golang/protobuf v1.3.1 // indirect
    github.com/mattn/go-isatty v0.0.7 // indirect
    github.com/ugorji/go v1.1.4 // indirect
    gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
    gopkg.in/yaml.v2 v2.2.2 // indirect

Require is all the dependent packages required by the gin framework, and the version number has been indicated after each dependent package

Upgrade dependency

First, we need to view the following list of dependencies we use

> $ go list -m all
//You will see the dependency packages used by all projects
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3
github.com/gin-gonic/gin v1.3.0
github.com/golang/protobuf v1.3.1
github.com/mattn/go-isatty v0.0.7
github.com/ugorji/go v1.1.4
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
gopkg.in/go-playground/validator.v8 v8.18.2
gopkg.in/yaml.v2 v2.2.2

Because the latest version is used here and cannot be upgraded, an example of fallback is given here. Roll back the version of the gin framework to the previous version. Here, you need to use a command to view the dependent version history.

>$ go list -m -versions github.com/gin-gonic/gin
//The gin version history will be listed
github.com/gin-gonic/gin v1.1.1 v1.1.2 v1.1.3 v1.1.4 v1.3.0

Update the version to the previous version. This is just a demonstration.

>$ go get github.com/gin-gonic/ [email protected] //Just add @ version after the dependency
>$ go list -m all
//See version changes
github.com/gin-gonic/gin v1.1.4

Or you can usego modTo switch versions, which requires two steps

>$ go mod edit -require="github.com/gin-gonic/ [email protected] "// modify the go.mod file
>$go tidy // download update dependency

go.tidyUnnecessary dependencies will be cleaned up automatically, and dependencies can be updated to the current version

It’s a very simple process to use. With only a few commands, you can know the dependent version information and freely choose the installed version. Everything becomes so simple.

Delete unused dependencies

If you need to remove some unnecessary dependencies during the project process, you can use the following command:

>$ go mod tidy

More about go mod commands

>$ go mod
The commands are:

        download    download modules to local cache
        edit        edit go.mod from tools or scripts
        graph       print module requirement graph
        init        initialize new module in current directory
        tidy        add missing and remove unused modules
        vendor      make vendored copy of dependencies
        verify      verify dependencies have expected content
        why         explain why packages or modules are needed


Go module is the future of go dependency management. At present, only versions 1.11 and 1.12 support this function. This paper introduces the function of go dependency management. More functions will be added later. You are also welcome to supplement and improve. Finally, if you are usingGoland, please move hereWorking with Go Modules
Read about developing with modules

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Summary of DOM event flow in JS

1、 Event capture 1. Concept Event capture: from the document to the node that triggered the event,Top downTo trigger the event. 2. Illustration Event capture 2、 Event bubbling 1. Concept Event bubbling: from the node that triggered the event to the document, yesBottom upTo trigger the event. 2. Illustration Event Bubbling 3、 DOM event flow […]