go moudules is a package management tool for Go. It is officially provided and is relatively reliable. , the minimum Go version required is 1.11.
can be understood as Python’s virtual environment, or Java’s maven, or PHP’s composer.
The essential problem it solves is the problem of package management confusion.
Anyway, just use it and that’s it. There are only good things and no bad things.
##Configuration (don’t worry)
GO111MODULE=off # 不适用modules功能 GO111MODULE=off #使用molules功能,不会去GOPATH下找依赖包, 1.11+版本默认开启 GO111MODULE=auto:Golang # Golang自己简则是不是使用modules功能
Based on the above command, it can be concluded that in version 1.11, go modules can be used by default, and no other settings are required.
What is the difference between it and the traditional direct go get
When go modules are not used, we directlygo get The downloaded package will be placed in the %GOPATH%/src directory by default.

After enabling go modules, the packages will be downloaded to %GOPATH%/pkg/mod folder.

And it will have a version number, which basically completely solves the dependency problem.
Get started
Create go.mod file
go.mod# The ## file is equivalent to the requirement.txt file, which will contain some required dependency packages.
Get started
I created agomod-demo folder.

andCMDenter this file Clip inside.

Execute the command go mod init gomod-demo.
The following gomod-demo can be written casually. It is recommended that it be the same as the folder name.

After success, there will be one more #go.modFile, represents success.

##I used Goland to open thisgomod-demo folder.

go.mod file description
module: module name.
require: Dependency package list and version.
exclude: prohibits dependent package list (only takes effect when the current module is the main module).
replace: Replace the dependent package list (only takes effect when the current module is the main module).
Of course, we don’t need to touch these basics, they are all modified through commands or IDE.
##Simple use
For example, I need to use a xstrings package.
github address:https://github.com/huandu/xstrings.
You need to execute this command to install this package.

执行下载命令。

go.mod文件会自动添加一个依赖包。

代码:
package main
import (
"fmt"
"github.com/huandu/xstrings"
)
func main() {
s := "heheh ADSL附近"
center := xstrings.Count(s, "h")
fmt.Println(center)
}
go连接Mysql
github文档如下。

执行下载命令 go get -u github.com/go-sql-driver/mysql

go.mod文件如下。

代码:
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"time"
)
func main() {
//s := "heheh ADSL附近"
//center := xstrings.Count(s, "h")
//fmt.Println(center)
db, err := sql.Open("mysql", "user:password@/dbname")
if err != nil {
panic(err)
}
// See "Important settings" section.
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
}
可能出现的异常
如果使用go get 第三方包时,goland无法导入,或者报错,或者没有Go Modules。

在项目目录下,使用go mod download下载一下,基本可以解决问题,只要有Go Modules,就代表没问题。

如果还是没有Go Modules,在Goland中settings->Go->GoModules(vgo)

go mod所有命令
在当前文件夹下初始化一个新的 module,创建 go.mod 文件:go mod init name 拉取缺少的模块,移除不用的模块:go mod tidy 将依赖复制到 vendor 下:go mod vendor 下载依赖:go mod download 检验依赖:go mod verify 显示模块依赖图:go mod graph 解释为什么需要依赖:go mod why 编辑 go.mod 文件:go eidt 查看命令列表:go mod 查看命令帮助文档:go help mod
The above is the detailed content of An article teaches you how to use Go language Modules. For more information, please follow other related articles on the PHP Chinese website!
Golang vs. Python: Concurrency and MultithreadingApr 17, 2025 am 12:20 AMGolang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.
Golang and C : The Trade-offs in PerformanceApr 17, 2025 am 12:18 AMThe performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.
Golang vs. Python: Applications and Use CasesApr 17, 2025 am 12:17 AMChooseGolangforhighperformanceandconcurrency,idealforbackendservicesandnetworkprogramming;selectPythonforrapiddevelopment,datascience,andmachinelearningduetoitsversatilityandextensivelibraries.
Golang vs. Python: Key Differences and SimilaritiesApr 17, 2025 am 12:15 AMGolang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.
Golang vs. Python: Ease of Use and Learning CurveApr 17, 2025 am 12:12 AMIn what aspects are Golang and Python easier to use and have a smoother learning curve? Golang is more suitable for high concurrency and high performance needs, and the learning curve is relatively gentle for developers with C language background. Python is more suitable for data science and rapid prototyping, and the learning curve is very smooth for beginners.
The Performance Race: Golang vs. CApr 16, 2025 am 12:07 AMGolang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.
Golang vs. C : Code Examples and Performance AnalysisApr 15, 2025 am 12:03 AMGolang is suitable for rapid development and concurrent programming, while C is more suitable for projects that require extreme performance and underlying control. 1) Golang's concurrency model simplifies concurrency programming through goroutine and channel. 2) C's template programming provides generic code and performance optimization. 3) Golang's garbage collection is convenient but may affect performance. C's memory management is complex but the control is fine.
Golang's Impact: Speed, Efficiency, and SimplicityApr 14, 2025 am 12:11 AMGoimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment







