当前位置:网站首页>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文件。
边栏推荐
- uniapp 除了数字,其他输入无效
- Compare two vis in LabVIEW
- 机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
- Pict generate orthogonal test cases tutorial
- How to choose a securities company? Is it safe to open an account on your mobile phone
- Qualcomm WLAN framework learning (30) -- components supporting dual sta
- Galera cluster of MariaDB - dual active and dual active installation settings
- 刷题指南-public
- [odx Studio Edit pdx] - 0.2 - Comment comparer deux fichiers pdx / odx
- ECS settings SSH key login
猜你喜欢

The difference between cout/cerr/clog

colResizable. JS auto adjust table width plug-in

Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record

智力考验看成语猜古诗句微信小程序源码

实战模拟│JWT 登录认证

VIM editor knowledge summary

The initial trial is the cross device model upgrade version of Ruijie switch (taking rg-s2952g-e as an example)

Stm32 Reverse Introduction to CTF Competition Interpretation

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

Redis introduction complete tutorial: List explanation
随机推荐
The caching feature of docker image and dockerfile
The initial trial is the cross device model upgrade version of Ruijie switch (taking rg-s2952g-e as an example)
MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class
Jar batch management gadget
Qt个人学习总结
uniapp 除了数字,其他输入无效
PS style JS webpage graffiti board plug-in
初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
一次edu证书站的挖掘
Advantages of Alibaba cloud international CDN
A complete tutorial for getting started with redis: understanding and using APIs
QT addition calculator (simple case)
法国学者:最优传输理论下对抗攻击可解释性探讨
Chinese verification of JS regular expressions (turn)
The small program vant tab component solves the problem of too much text and incomplete display
[crawler] jsonpath for data extraction
Excel 快捷键-随时补充
Redis: redis transactions
股票账户佣金怎么调低,炒股佣金怎么调低网上开户安全吗
金融市场,资产管理与投资基金