当前位置:网站首页>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 !
边栏推荐
- Tencent wetest platform will bring new benefits in 2021 with 618 special offers!
- What does ERP system mean
- Step 3: access the API interface for inquiry of SF express doc No. [express 100api interface]
- Fais ce que tu veux.
- SQL Server about like operator (including the problem of field data automatically filling in spaces)
- Spark submission parameter -- use of files
- Illustration miscellaneous [for archiving to prevent loss]
- Dedecms template file explanation and homepage label replacement
- 【IEEE】自然语言处理与信息检索国际会议(ECNLPIR 2022)
- [JS reverse sharing] community information of a website
猜你喜欢
随机推荐
Niuke-top101-bm28
09. Tencent cloud IOT device side learning -- RRPC and behavior
[technical tutorial] national standard protocol platform easygbs cascading supports customized national standard channels
MYSQL_精讲数据库数据类型
Reliable remote code execution (1)
机械臂速成小指南(一):机械臂发展概况
Use the process monitor tool to monitor process operations on registries and files
腾讯开源项目「应龙」成Apache顶级项目:前身长期服务微信支付,能hold住百万亿级数据流处理...
脚本之美│VBS 入门交互实战
Niuke-top101-bm29
Learn how to use PHP to filter special symbols in strings
What is recursion?
Why use a firewall? What is the function of firewall?
喜欢就去行动
Today's sleep quality record 76 points
What is the bin file for? How to open the file correctly
JMeter interface test tool foundation - sampler (II)
JMeter interface test tool foundation - badboy tool
Fais ce que tu veux.
Dedecms template file explanation and homepage label replacement