[series] using go modules package management tool (1)



  • Summary
  • Initialization
  • Add dependency package
  • Go mod command
  • Summary
  • Source address


I want to implement the wheel of an out of the box API framework, which is based on gin.

Why is it out of the box? What functions will it integrate?

[series] using go modules package management tool (1)

The above function points are commonly used and may be added later.

Don’t talk too much. Let’s start.

To create a project, we first need to consider a package dependent management tool.

Common package management includes DEP, go vendor, glide, go modules, etc.

At the beginning, I used DEP, but my friend dis recommended me to use go modules.

Now let’s talk about go modules. This is with the release of go 1.11. This is the new package management advocated by the government.

Say an environment variable: go111module. The default value is auto.

When there is go.mod in the project, use go modules to manage, otherwise use the old gopath and vendor mechanism.

If you want to use go modules, you can set go111module to on.

Go straight to it.


Let’s create an empty folder outside of gopathgo-gin-api

cd go-gin-api && go mod init go-gin-api


go: creating new go.mod: module go-gin-api

At this time, there is a go.mod file in the directory. The contents are as follows:

module go-gin-api

go 1.12

At this point, go mod initialization is complete. Next, add dependency package – gin.

Add dependency package

Create amain.goPut the following code on the file:

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

This code is nothing special. It’s the official entry demo.

Next, download the dependency package.

go mod tidy

After execution, take a lookgo.modDocument:

module go-gin-api

go 1.12

require github.com/gin-gonic/gin v1.4.0

At this time, you will see a new package of gin v1.4.0.

It also generates a go. Sum file, which can be temporarily ignored.

Two problems were found.

1. No gin package is found in the directory. Where is the package downloaded?

Downloaded to the gopath / PKG / mod directory.

2. The reference of gin in GoLand editor turns red?

Here the editor needs to be set as follows:

[series] using go modules package management tool (1)

Click apply and OK.

If this does not work, you can also perform:

go mod vendor

This command is to put the package that the project depends on into the vendor directory of the project, which is certainly OK.

Go mod command

go mod tidy

Pull the missing module and remove the unused module.

I often use this command.

go mod vendor

Copy the dependency to vendor.

I often use this command.

go mod download

Download the dependency package.

go mod verify

Test dependency.

go mod graph

Print module dependency graph.

Other commands, which can be executedgo mod, just view.


This article shares the use of go modules.

  • Build a project from scratch using go modules.
  • The GoLand editor uses go modules.

So far today, the next article starts to build the API project and write parameter verification.

Source address


Recommended Today

The initial use of Dao layer + service layer + action layer in spring

1. Dao layer: (divided into interface and Implementation) package com.myget; public interface PersonDao { public void add(); } + package com.myget; import javax.annotation.Resource; import org.springframework.stereotype.Repository; @Repository (“persondao”) / / equivalent topublic class PersonDaoImplement implements PersonDao { public void add(){ System.out.println (the “add method of Dao layer”); }} 2. Service layer: (interface and Implementation) package com.myget; […]