当前位置:网站首页>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文件。
边栏推荐
- 企业里Win10 开启BitLocker锁定磁盘,如何备份系统,当系统出现问题又如何恢复,快速恢复又兼顾系统安全(远程设备篇)
- Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
- MariaDB的Galera集群应用场景--数据库多主多活
- JS 3D explosive fragment image switching JS special effect
- The initial arrangement of particles in SPH (solved by two pictures)
- ScriptableObject
- 微软禁用IE浏览器后,打开IE浏览器闪退解决办法
- [JS] - [dynamic planning] - Notes
- Basic knowledge of database
- Redis getting started complete tutorial: hash description
猜你喜欢
可观测|时序数据降采样在Prometheus实践复盘
qt绘制网络拓补图(连接数据库,递归函数,无限绘制,可拖动节点)
JS card style countdown days
Jar批量管理小工具
Solution record of jamming when using CAD to move bricks in high configuration notebook
如何用快解析自制IoT云平台
Qualcomm WLAN framework learning (30) -- components supporting dual sta
快解析内网穿透帮助企业快速实现协同办公
Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record
Editplus-- usage -- shortcut key / configuration / background color / font size
随机推荐
Network namespace
[JS] - [dynamic planning] - Notes
一次edu证书站的挖掘
MP进阶操作: 时间操作, sql,querywapper,lambdaQueryWapper(条件构造器)快速筛选 枚举类
MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class
QT addition calculator (simple case)
After Microsoft disables the IE browser, open the IE browser to flash back the solution
ScriptableObject
蓝天NH55系列笔记本内存读写速度奇慢解决过程记录
QT personal learning summary
字体设计符号组合多功能微信小程序源码
The difference between cout/cerr/clog
快解析内网穿透帮助企业快速实现协同办公
时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
Servlet服务器端和客户端中文输出乱码问题
实战模拟│JWT 登录认证
高通WLAN框架学习(30)-- 支持双STA的组件
数据库基础知识
ETCD数据库源码分析——处理Entry记录简要流程
Chinese verification of JS regular expressions (turn)