当前位置:网站首页>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 !
边栏推荐
- 计组_cpu的结构和工作流程
- math_等比数列求和推导&等幂和差推导/两个n次方数之差/
- Any 与 TypeVar,让 IDE 的自动补全更好用
- The latest entry date of SQL Sever test questions
- First acquaintance with string+ simple usage (I)
- Fashionable pop-up mode login registration window
- 機械臂速成小指南(二):機械臂的應用
- What is the bin file for? How to open the file correctly
- [IEEE publication] International Conference on natural language processing and information retrieval in 2022 (ecnlpir 2022)
- 齐次坐标的理解
猜你喜欢

Canvas pipe animation JS special effect

Use the process monitor tool to monitor process operations on registries and files

Fashionable pop-up mode login registration window

@RequestBody注解
![[data analysis data source] coordinates of provinces, cities and administrative regions across the country (including boundary coordinate points and central coordinate points)](/img/a8/84088b1e61deaf62f22d85a007423b.png)
[data analysis data source] coordinates of provinces, cities and administrative regions across the country (including boundary coordinate points and central coordinate points)

服乔布斯不服库克,苹果传奇设计团队解散内幕曝光

机械臂速成小指南(二):机械臂的应用

機械臂速成小指南(二):機械臂的應用

把腾讯搬到云上,治愈了他们的技术焦虑

解决DBeaver SQL Client 连接phoenix查询超时
随机推荐
What is recursion?
Lightweight deployment of firefoxsend temporary file sharing service using Tencent cloud
Reliable remote code execution (1)
喜欢就去行动
SQL Server about like operator (including the problem of field data automatically filling in spaces)
First acquaintance with string+ simple usage (I)
Maui's way of learning -- Opening
Cool interactive animation JS special effects implemented by p5.js
计组_cpu的结构和工作流程
SwiftUI Swift 内功之 Swift 中的属性观察者 didSet 与 willSet
Detailed explanation of SQL Sever basic data types
System design: load balancing
Act as you like
Introduction to the use of splice() method
What is a compressed file? What are the advantages of different methods of compressing files?
Which map navigation is easy to use and accurate?
Learn how to use PHP to filter special symbols in strings
使用Process Monitor工具监测进程对注册表和文件的操作
初识string+简单用法(一)
Several stacks of technology sharing: product managers' Online Official answers to several stacks of knowledge