我想使用这个包:github.com/go-sql-driver/mysql
我尝试过:
go get -u github.com/go-sql-driver/mysql它运行正常,但当我导入包并运行代码时,它给了我一个错误:
main.go:12:2: no required module provides package github.com/go-sql-driver/mysql: go.mod file not found in current directory or any parent directory; see 'go help modules'发布于 2021-09-20 13:00:19
在项目根目录中运行
go mod init example.com/your-project-name它将创建go.mod文件
module example.com/your-project-name
go 1.15然后运行
go get -u github.com/go-sql-driver/mysql发布于 2021-09-20 17:42:19
编辑:正如rustyx在评论中所说,这是一种被弃用的方法,你可能想要考虑新的方法
如果正确设置了GOROOT和GOPATH环境变量,则可以不使用go.mod
为此,您可以参考What should be the values of GOPATH and GOROOT?
此外,还需要确保IDE/文本编辑器也能识别这两个变量。
注意:对于IDE (例如Goland),您可能需要手动设置它。在使用文本编辑器(例如: vscode)的情况下,您只需重新加载您的项目,它就可以完成此任务。
完成此操作后,您可以验证$GOPATH/github.com/go-sql-driver/mysql/的内容是否存在。如果没有,您可以重新运行go get -u github.com/go-sql-driver/mysql命令,重新加载编辑器,这样就可以了
https://stackoverflow.com/questions/69253648
复制相似问题