当前位置:网站首页>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文件。
边栏推荐
- D3.js+Three. JS data visualization 3D Earth JS special effect
- 取得PMP证书需要多长时间?
- HMS core unified scanning service
- Tweenmax emoticon button JS special effect
- The small program vant tab component solves the problem of too much text and incomplete display
- A complete tutorial for getting started with redis: redis usage scenarios
- MIT-6.824-lab4B-2022(万字思路讲解-代码构建)
- QT drawing network topology diagram (connecting database, recursive function, infinite drawing, dragging nodes)
- Actual combat simulation │ JWT login authentication
- Object detection based on OpenCV haarcascades
猜你喜欢

Selected cutting-edge technical articles of Bi Ren Academy of science and technology

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

heatmap. JS picture hotspot heat map plug-in

debug和release的区别

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

A complete tutorial for getting started with redis: getting to know redis for the first time

实战模拟│JWT 登录认证

French scholars: the explicability of counter attack under optimal transmission theory

Solution record of jamming when using CAD to move bricks in high configuration notebook

MariaDB的Galera集群应用场景--数据库多主多活
随机推荐
Why does infographic help your SEO
How long does it take to obtain a PMP certificate?
The caching feature of docker image and dockerfile
In the enterprise, win10 turns on BitLocker to lock the disk, how to back up the system, how to recover when the system has problems, and how to recover quickly while taking into account system securi
Font design symbol combination multifunctional wechat applet source code
PaddleOCR教程
CTF竞赛题解之stm32逆向入门
The small program vant tab component solves the problem of too much text and incomplete display
Actual combat simulation │ JWT login authentication
[Taichi] change pbf2d (position based fluid simulation) of Taiji to pbf3d with minimal modification
C语言快速解决反转链表
【爬虫】数据提取之JSONpath
S32 Design Studio for ARM 2.2 快速入门
一次edu证书站的挖掘
The difference between debug and release
HMS core machine learning service
【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
Solve the problem that the virtual machine cannot be remotely connected through SSH service
智力考验看成语猜古诗句微信小程序源码
ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design