当前位置:网站首页>Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
2022-07-04 23:28:00 【Xiaoming's note warehouse】
background
Prepare to run the following code
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
// Create a default routing engine
r := gin.Default()
// GET: Request mode ;/hello: The path of the request
// When the client uses GET Method request /hello When the path , Will execute the following anonymous function
r.GET("/hello", func(c *gin.Context) {
// c.JSON: return JSON Formatted data
c.JSON(200, gin.H{
"message": "Hello world!",
})
})
// start-up HTTP service , Default in 0.0.0.0:8080 Start the service
r.Run()
}
The result is wrong :
no required module provides package : go.mod file not found in current directory or any parent directory;
That is to say, the package of the imported module cannot be found
Try 1:
go get -u github.com/gin-gonic/gin
There is no effect
Online is basically this solution : perform :go env -w GO111MODULE=auto
But mine is already open , So exclude this solution .
Try 3: I built another project , Another running error result is found :
1.go:4:2: no required module provides package github.com/gin-gonic/gin; to add it:
go get github.com/gin-gonic/gin
Pay attention to the difference between the two error reports , This prompt go get github.com/gin-gonic/gin
It shows that the original project is lack of things , I compared it and found that the folder where the original project code is located is missing go.mod This file
Through research Go go.mod Detailed explanation _ The blog of Min Ming, the owner of Jiangdong flower -CSDN Blog _go.mod file
Got the solution
resolvent
Open the terminal in the error code folder , function
go mod init New module name
You can initialize go.mod, Run it like this
go get -u github.com/gin-gonic/gin
Just OK 了
summary
- Go.mod yes Golang1.11 The official package management tool newly introduced by the version is used to solve the problem that there is no place to record the specific version of the package , Facilitate the management of dependent packages .
- Go.mod It's really just a Modules, About Modules The official definition of is :Modules It's related Go Bag aggregate , Is the source code exchange and version control unit .go Command direct support Modules, Including logging and parsing dependencies on other modules .Modules Replace old based on GOPATH Methods , To specify which source files to use .
- Modules And traditional GOPATH Different , It is not necessary to include, for example src,bin Such subdirectories , A source directory or even an empty directory can be used as Modules, As long as it contains go.mod file .
边栏推荐
猜你喜欢
Hash table, hash function, bloom filter, consistency hash
香港珠宝大亨,22亿“抄底”佐丹奴
[binary tree] the maximum difference between a node and its ancestor
Phpcms paid reading function Alipay payment
The initial trial is the cross device model upgrade version of Ruijie switch (taking rg-s2952g-e as an example)
为什么信息图会帮助你的SEO
Etcd database source code analysis - brief process of processing entry records
Qualcomm WLAN framework learning (30) -- components supporting dual sta
Combien de temps faut - il pour obtenir un certificat PMP?
Pytoch --- use pytoch to realize linknet for semantic segmentation
随机推荐
How to apply for PMP project management certification examination?
Intelligence test to see idioms guess ancient poems wechat applet source code
高通WLAN框架学习(30)-- 支持双STA的组件
CTF竞赛题解之stm32逆向入门
Pytoch --- use pytoch to realize linknet for semantic segmentation
[JS] - [sort related] - Notes
智力考验看成语猜古诗句微信小程序源码
Combien de temps faut - il pour obtenir un certificat PMP?
Using the uniapp rich text editor
【js】-【排序-相关】-笔记
Basic use and upgrade of Android native database
The difference between debug and release
Meet ThreadPoolExecutor
Chinese verification of JS regular expressions (turn)
Network namespace
华泰证券低佣金的开户链接安全吗?
Jar batch management gadget
Excel 快捷键-随时补充
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
QT personal learning summary