Ubuntu 找不到golang编译简单应用程序模块(GOPATH?)

Ubuntu 找不到golang编译简单应用程序模块(GOPATH?),ubuntu,go,gopath,Ubuntu,Go,Gopath,刚开始使用go时,遇到了一个非常简单的应用程序(从这里开始) 当我尝试编译hello.go时: ... hello.go:6:5: cannot find module providing package example.com/greeting ... 环境设置如下(Ubuntu 20.04) 源代码 请帮助,安装有什么问题?您需要使用replace指令指向程序包的本地路径。否则,Go会尝试在实际路径所在的位置找到它——在example.com/greeting。在您链接到的页面中,会提到:

刚开始使用go时,遇到了一个非常简单的应用程序(从这里开始) 当我尝试编译hello.go时:

...
hello.go:6:5: cannot find module providing package example.com/greeting
...
环境设置如下(Ubuntu 20.04)

源代码


请帮助,安装有什么问题?

您需要使用
replace
指令指向程序包的本地路径。否则,Go会尝试在实际路径所在的位置找到它——在
example.com/greeting
。在您链接到的页面中,会提到:


对于生产使用,您可以在公司内部或互联网上的服务器上发布模块,Go命令将从那里下载模块。现在,您需要调整调用者的模块,以便它可以在本地文件系统上找到问候语代码

为此,对hello模块的go.mod文件做一个小更改

在hello目录中,打开go.mod文件,将其更改为如下所示,然后保存该文件

module hello

go 1.14

replace example.com/greetings => ../greetings

事实上,因为您使用的是模块(go 1.15也应该如此!),所以我建议您完全不要设置。它不需要,只是在“模块模式”中增加了混乱

module hello

go 1.14

replace example.com/greetings => ../greetings