当前位置:网站首页>go踩坑——no required module provides package : go.mod file not found in current directory or any parent
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
2022-07-04 23:28:00 【小明的筆記倉庫】
背景
准備運行下面代碼
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
// 創建一個默認的路由引擎
r := gin.Default()
// GET:請求方式;/hello:請求的路徑
// 當客戶端以GET方法請求/hello路徑時,會執行後面的匿名函數
r.GET("/hello", func(c *gin.Context) {
// c.JSON:返回JSON格式的數據
c.JSON(200, gin.H{
"message": "Hello world!",
})
})
// 啟動HTTP服務,默認在0.0.0.0:8080啟動服務
r.Run()
}結果報錯:
no required module provides package : go.mod file not found in current directory or any parent directory;
也就是說找不到導入模塊的包
嘗試1:
go get -u github.com/gin-gonic/gin沒有效果
嘗試2:關於golang第三方包的引用報錯:no required module provides package_編程哲學家的博客-CSDN博客
網上基本都是這個解决方案:執行:go env -w GO111MODULE=auto
但是我的已經打開了,所以排除這個解决方案。
嘗試3:我又重新建了個項目,發現另一個運行時報錯結果是:
1.go:4:2: no required module provides package github.com/gin-gonic/gin; to add it:
go get github.com/gin-gonic/gin
注意這兩個報錯的區別,這個提示了 go get github.com/gin-gonic/gin
說明原項目是缺少東西的,我對比了一下發現原項目代碼所在的文件夾中缺少go.mod這個文件
通過研究Go go.mod詳解_絳洞花主敏明的博客-CSDN博客_go.mod文件
得到了解决方案
解决方法
在報錯的代碼文件夾打開終端,運行
go mod init 新模塊名字就可以初始化go.mod,這樣再運行
go get -u github.com/gin-gonic/gin就OK了
總結
- Go.mod是Golang1.11版本新引入的官方包管理工具用於解决之前沒有地方記錄依賴包具體版本的問題,方便依賴包的管理。
- Go.mod其實就是一個Modules,關於Modules的官方定義為:Modules是相關Go包的集合,是源代碼交換和版本控制的單元。go命令直接支持使用Modules,包括記錄和解析對其他模塊的依賴性。Modules替換舊的基於GOPATH的方法,來指定使用哪些源文件。
- Modules和傳統的GOPATH不同,不需要包含例如src,bin這樣的子目錄,一個源代碼目錄甚至是空目錄都可以作為Modules,只要其中包含有go.mod文件。
边栏推荐
- Summary of wechat applet display style knowledge points
- 华泰证券低佣金的开户链接安全吗?
- qt绘制网络拓补图(连接数据库,递归函数,无限绘制,可拖动节点)
- 壁仞科技研究院前沿技术文章精选
- 快解析——好用的内网安全软件
- Application of machine learning in housing price prediction
- 刷题指南-public
- 【kotlin】第三天
- phpcms付费阅读功能支付宝支付
- Selected cutting-edge technical articles of Bi Ren Academy of science and technology
猜你喜欢

Editplus-- usage -- shortcut key / configuration / background color / font size

【js】-【排序-相关】-笔记

The small program vant tab component solves the problem of too much text and incomplete display

C language to quickly solve the reverse linked list

ICML 2022 || 3DLinker: 用于分子链接设计的E(3)等变变分自编码器

S32 design studio for arm 2.2 quick start

智力考验看成语猜古诗句微信小程序源码
![[binary tree] the maximum difference between a node and its ancestor](/img/b5/1bc3d102754fc44c6a547807ebab94.png)
[binary tree] the maximum difference between a node and its ancestor

D3.js+Three. JS data visualization 3D Earth JS special effect
![[Jianzhi offer] 6-10 questions](/img/73/5974068008bcdc9a70b3f5f57f1eb0.png)
[Jianzhi offer] 6-10 questions
随机推荐
【js】-【排序-相关】-笔记
时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
SPH中的粒子初始排列问题(两张图解决)
Excel shortcut keys - always add
How to reduce the stock account Commission and stock speculation commission? Is it safe to open an online account
Observable time series data downsampling practice in Prometheus
A mining of edu certificate station
Why does infographic help your SEO
S32 design studio for arm 2.2 quick start
Chinese verification of JS regular expressions (turn)
Application of machine learning in housing price prediction
Phpcms paid reading function Alipay payment
Stm32 Reverse Introduction to CTF Competition Interpretation
机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
JS card style countdown days
A complete tutorial for getting started with redis: understanding and using APIs
【爬虫】数据提取之JSONpath
Solution record of jamming when using CAD to move bricks in high configuration notebook
刷题指南-public
[graph theory] topological sorting