当前位置:网站首页>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:16: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文件。
边栏推荐
- A complete tutorial for getting started with redis: getting to know redis for the first time
- 【kotlin】第三天
- CTF競賽題解之stm32逆向入門
- 初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
- MariaDB的Galera集群-双主双活安装设置
- Pict generate orthogonal test cases tutorial
- UML diagram memory skills
- MySQL数据库备份与恢复--mysqldump命令
- Stm32 Reverse Introduction to CTF Competition Interpretation
- HMS core unified scanning service
猜你喜欢
ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
QT personal learning summary
Redis introduction complete tutorial: detailed explanation of ordered collection
debug和release的区别
Etcd database source code analysis - brief process of processing entry records
Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record
蓝天NH55系列笔记本内存读写速度奇慢解决过程记录
Redis:Redis的事务
ETCD数据库源码分析——处理Entry记录简要流程
Redis:Redis消息的发布与订阅(了解)
随机推荐
The initial arrangement of particles in SPH (solved by two pictures)
ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
Mit-6.824-lab4b-2022 (10000 word idea explanation - code construction)
Application of machine learning in housing price prediction
C language to quickly solve the reverse linked list
Chinese verification of JS regular expressions (turn)
字体设计符号组合多功能微信小程序源码
The difference between debug and release
Recommended collection: build a cross cloud data warehouse environment, which is particularly dry!
如何报考PMP项目管理认证考试?
实战模拟│JWT 登录认证
Qt加法计算器(简单案例)
French scholars: the explicability of counter attack under optimal transmission theory
The caching feature of docker image and dockerfile
Meet ThreadPoolExecutor
ECCV 2022 | Tencent Youtu proposed disco: the effect of saving small models in self supervised learning
金融市场,资产管理与投资基金
Solution record of jamming when using CAD to move bricks in high configuration notebook
cout/cerr/clog的区别
Servlet+JDBC+MySQL简单web练习