当前位置:网站首页>Go basic series | 4 Environment construction (Supplement) - gomod doubts

Go basic series | 4 Environment construction (Supplement) - gomod doubts

2022-06-24 11:02:00 Handsome brother and black Marshal

selected from “ Handsome brother and black Marshal ” official account , A love programmer official account , You can scan the code at the end of the article to pay attention to .

​ This article is about the problems that people encounter when building an environment gomod Explain the doubts caused by the problem .

What is? gomod

full name go modules, In short, it is go Project tools for managing third-party packages . This feature is golang 1.11 The version began to add .

Three settings

stay ”Go Foundation Series | Environment building “ In that article , Explained the settings GO111MODULE The environment variable is on, If you don't understand, you can read that article , There will be explanations for different systems . but GO111MODULE Environment variables actually have three values that can be set , As follows :

  • on: Force enable gomod Manage third party packages , Therefore, the project directory must contain go.mod file .
  • off: Through the current project vendor Catalog or GOPATH Pattern to find .
  • auto: If there is... Under the current project go.mod file , Just use on In the form of , There is no adoption off In the form of .

notes : In actual development, it is recommended to set it to on

Common command

1. initialization

Initialization command , Will generate... In the current directory go.mod file , In a project , Only one copy of this document is required , Placed in the project root directory .

go mod init 
#  Customize  module  name 
go mod init study

go.mod The contents are as follows :

#  The current directory is study, Automatic generation 
module study

go 1.16

2. Auto update

Automatically download the third-party packages that the current project depends on , It will generate automatically go.sum file , This file contains all the specific versions of the third-party packages that the current project depends on . If a dependent package is deleted from the project , Also from the go.mod Delete... From file .

go mod tidy

3. Copy vendor

Automatically copy dependent third-party packages to vendor Under the table of contents .

go mod vendor

4. Other

These are the commands I often use , The remaining commands are rarely used .

  • go mod download: Download dependency package , If the current project is not imported, it will not be downloaded , The following is a practical operation
  • go mod edit: edit go.mod
  • go mod graph: Print module dependency graph
  • go mod verify: Verify that the dependency is correct
  • go mod why: Explain why you need to rely

Goland Introduction package

Tell me about a process in my actual development , If you use now Goland Editor to write code , And it's turned on gomod, How to introduce a new package , The process is as follows .

1. download

Under your own project , Use go get Command download package , If you download gin frame .

#  Pull the latest 
go get github.com/gin-gonic/gin

After completion of operation , Will be in go.mod This package is imported into the file , The contents of the document are as follows :

module study

go 1.16

require github.com/gin-gonic/gin v1.7.2 // indirect

You can also download the specified version

#  Specific version , It's followed by  git  Set in the tag
go get github.com/gin-gonic/[email protected]

#  Pull a specific commit,commit id It can be abbreviated as 
go get  github.com/gin-gonic/[email protected]

2. Turn on gomod Package tips

Click in the upper left corner File,Setting > Go > Go modules , Turn on Enable Go modules integration. After opening, you will be prompted when calling the package , And through ctrl + Left mouse button , View source code .

3. Auto import

The premise is that goimport Tools , stay ”Go Foundation Series | Environment building “ One article explains how to configure , Come down and start writing code .

When writing part of the code , It will automatically prompt , After entering, it will automatically introduce github.com/gin-gonic/gin package , The complete code is as follows :

package main

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

func main() {
    gin.New()
}

further

Master the above knowledge , Should not be stuck outside the door in the introductory stage , For now go.mod Make an overall understanding of the contents of the document .

//  Project name , The way others introduce you to this project 
module github.com/a/b

//  The version number of the current environment 
go 1.16

//  rely on 
//  appear  indirect  Indicates that... Has not been used in the project 
require (
        one/thing v1.3.2
        other/thing v2.5.0 // indirect
        ...
)

//  Exclude specific versions 
//  If a version appears important bug, You can do that 
exclude (
        bad/thing v0.7.3
)

//  replace 
replace (
//  Project approval src/thing introduce , But the actual introduction position is dst/thing, Equivalent to taking an alias 
        src/thing 1.0.2 => dst/thing v1.1.0
)

Minimum version selection

If the same package is introduced in the current project and the dependent third-party package , But not the same version , At this time, a minimum version will be selected to import .

summary

For the above knowledge , I think it is enough in actual development , If you encounter problems not mentioned in my article , But I don't know how to solve it , Leave a message below !

原网站

版权声明
本文为[Handsome brother and black Marshal]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210610185432597o.html