This is the 16th day of my participation in Gwen Challenge

Go engineering and dependency management basic mechanisms

Changes in dependency management mechanisms

GoPath—>GoVendor—->GoMod

  • GoPath: The project code is required to be stored in the GoPath/ SRC directory. Also require your dependency library to be under this directory:
$GOPATH/src/yourProject/

$GOPATH/ SRC /gorm: if gorM is relied on
Copy the code

The code in the GoPath/ SRC directory is used directly when compiling. The code downloaded by Go Get will also be placed in GoPath.

Go env = Go env = Go env = Go env

│ ├─ DLV │ ├─ Go - Outline │ ├─ GomodifyTags │ ├─ goPlay │ ├─ gopls │ ├─ Gotests │ ├── └─ staticCheck ├─ ├─ goTests │ ├─ ├── goTests │ ├─ ├─ ├─ gotests │ ├─ ├─ ├─ gotests │ ├─ ├─ ├─ gotests │ ├─ ├─ ├─ gotests │ ├─ ├─ ├─Copy the code

This structure, combined with GoPath’s requirements, requires us to write our code in a SRC file. Or you can write code in any other directory, but the binary that you build will be in SRC before it can be executed.

As mentioned earlier, Go Get downloads will also be placed in GoPath, so your project code and third-party code will be in the same directory, and even your multiple project code will be in the same GoPath directory, which is confusing.

To solve this problem (using GoPath again), we will specify a new GoPath for each project, so that the project management is clear and everyone is separated. Whereas third-party libraries, you have to download them every time.

  • GoVendor

Enable govendor by running the following command:

export GO15VENDOREXPEIMENT=1
Copy the code

The effect is that all dependent packages will be downloaded under the./vendor directory. The dependency packages in the directory are also used during compilation, and the go vendor command is used to update the dependency packages. This solves the problem of mixing dependencies and project code, but it does not solve the problem of mixing multiple project code.

  • Go Mod
exportGO111MODULE=on // Enable go modCopy the code

The essence is to use the go.mod file to describe the name and version of our dependent library. Download dependencies will be placed in

$GOPAHT/ PKG /mod folderCopy the code

Also, the project code doesn’t have to be placed under GOPATH.

However, in enterprise practice, we still recommend that all projects be placed in the GOPATH directory, so that the project structure is more clear, that is, Govendor is actually similar to go mod effect.

How does Go Mod work

  • Gomod version expression
  1. Semantic version, commonly known as V1.0.3
v${major}.${minor}.${patch}Major, on the other hand, thinks of two different warehousesCopy the code
  1. Pseudo version number

This can be done based on a COMMIT, for example:

Xx/XXX/XXX v0.0.0-20210323104329-2dfsdfsdf xx/ XXX/XXX v0.0.0-20210323104329-2DFsdfsdf Base version prefix commit UTC time commit Hash before 12Copy the code
  1. Main command
Go get XXXX dependency library - go get Dependency library can keep up with a specific commit hash as well as a branch for example: -go get dependency library @hash -go get dependency library @master go mod Tidy // Automatically add or remove project dependent librariesCopy the code
  • Gomod version selection mechanism

Q: X depends on a, B, A and B both depend on C, but A depends on V1.1.1 of C and B depends on V1.2.2 of C. What version of C is used when x is compiled?

The answer is the highest version, V1.2.2. Minimal Version Selection (MVS)

! [image-20210626155846867](/Users/bytedance/Library/Application Support/typora-user-images/image-20210626155846867.png)

Engineering and dependency management FaQs

  • In the Go mod file we see some flags:

    • // indirect

    This dependency is not a direct dependency in this project, but is specified in this project. (A depends on B, b depends on C, in a can specify what version c is)

    • // incompatible

    This dependency does not use gomod to manage dependencies and does not affect usage

  • Some tools

Go Mod Graph: View relationships before dependencies

Go Mod vendor: Place the dependent files in the vendor folder under the project directory, similar to go Vendor. Generally, there is no need to submit the file under vendor, which is mainly used to check and jump the code.

  • Temporary changes depend on the code in the library for testing

Git clone to local, checkout the corresponding version, go mod file replace

A classic case in business practice

  1. Go get -u will pull the dependency, may cause some unexpected problems, do not add -u
  2. If a dependency does not use a mod, the default is to pull the latest version; It is recommended that our new project use mods with or without dependencies
  3. The tag of the dependent item A is deleted. What should I do
    • If it’s just your project, go get again and get the latest version
    • Replace the A project on your project if not only your project, but also your dependency B depends on the A project. In the long run, go get your dependency back.