当前位置:网站首页>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文件。
边栏推荐
- Docker镜像的缓存特性和Dockerfile
- ECCV 2022 | 腾讯优图提出DisCo:拯救小模型在自监督学习中的效果
- LabVIEW中比较两个VI
- 如何在外地外网电脑远程公司项目?
- Financial markets, asset management and investment funds
- Meet ThreadPoolExecutor
- Redis getting started complete tutorial: hash description
- 企业里Win10 开启BitLocker锁定磁盘,如何备份系统,当系统出现问题又如何恢复,快速恢复又兼顾系统安全(远程设备篇)
- 一次edu证书站的挖掘
- [sword finger offer] questions 1-5
猜你喜欢

取得PMP证书需要多长时间?

Redis: redis transactions

【kotlin】第三天

The caching feature of docker image and dockerfile

【二叉树】节点与其祖先之间的最大差值

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

快解析内网穿透帮助企业快速实现协同办公

机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)

MIT-6.824-lab4B-2022(万字思路讲解-代码构建)

Phpcms paid reading function Alipay payment
随机推荐
LabVIEW中比较两个VI
Editplus-- usage -- shortcut key / configuration / background color / font size
The input of uniapp is invalid except for numbers
CTF竞赛题解之stm32逆向入门
PS style JS webpage graffiti board plug-in
认识ThreadPoolExecutor
Font design symbol combination multifunctional wechat applet source code
Qt个人学习总结
phpcms付费阅读功能支付宝支付
ETCD数据库源码分析——处理Entry记录简要流程
[kotlin] the third day
【js】-【动态规划】-笔记
Mit-6.824-lab4b-2022 (10000 word idea explanation - code construction)
Header file duplicate definition problem solving "c1014 error“
A complete tutorial for getting started with redis: understanding and using APIs
A complete tutorial for getting started with redis: redis usage scenarios
[sword finger offer] questions 1-5
Why does infographic help your SEO
高通WLAN框架学习(30)-- 支持双STA的组件
机器学习在房屋价格预测上的应用