go mod 无法找到模块

huangapple go评论87阅读模式
英文:

go mod unable to find modules

问题

我正在尝试使用一个已编译的Go二进制文件作为ENTRYPOINT来构建一个Docker镜像,但是由于go mod无法找到所需的文件之一,所以无法编译该二进制文件。

项目结构如下所示:

editor/
├── container
│   ├── Dockerfile
│   └── src
│       ├── install-browsers.sh
│       ├── selenium-server-standalone-3.141.59.jar
│       └── webCopy
│           ├── go.mod
│           ├── go.sum
│           └── main.go
├── copier
│   ├── copier.go
│   ├── internal
│   │   └── utils.go
│   └── scripts
│       └── load.go
└── resource
    └── handler.go

我正在尝试编译的文件是webCopy/main.go
在该文件中,我需要导入editor/copier模块。

editor模块的路径是:
bitbucket.org/backend/editor
它位于GOPATH中

go mod tidy给出的错误是:

go: finding module for package bitbucket.org/mvps/backend/editor/copier
bitbucket.org/backend/editor/container/src/webCopy imports
        bitbucket.org/backend/editor/copier: cannot find module providing package bitbucket.org/mvps/backend/editor/copier: reading https://api.bitbucket.org/2.0/repositories/mvps/backend?fields=scm: 404 Not Found

我真的不想将copier模块混合到容器的src目录中,原因是我觉得子模块应该与主模块分开,而不是在editor模块内部。

此外,我正在使用go.mod来通过编译main.go并使用二进制文件创建一个新的干净的构件,所以我希望将go.mod和go.sum文件放在editor/container/src/webCopy/目录中。

顺便说一句,我已经检查过包名,一切都命名正确。

英文:

I'm trying to build a Docker image using a go compiled binary as the ENTRYPOINT but I'm unable to compile the binary because the go mod is unable to find one of the required files.

the project structure looks like this:

editor/
├── container
│   ├── Dockerfile
│   └── src
│       ├── install-browsers.sh
│       ├── selenium-server-standalone-3.141.59.jar
│       └── webCopy
│           ├── go.mod
│           ├── go.sum
│           └── main.go
├── copier
│   ├── copier.go
│   ├── internal
│   │   └── utils.go
│   └── scripts
│       └── load.go
└── resource
    └── handler.go

The file I'm trying to compile is webCopy/main.go
Inside that file I need to import the module editor/copier

The path to the editor module is:
bitbucket.org/backend/editor
which is inside the GOPATH

The error the go mod tidy gives me is:

go: finding module for package bitbucket.org/mvps/backend/editor/copier
bitbucket.org/backend/editor/container/src/webCopy imports
        bitbucket.org/backend/editor/copier: cannot find module providing package bitbucket.org/mvps/backend/editor/copier: reading https://api.bitbucket.org/2.0/repositories/mvps/backend?fields=scm: 404 Not Found

I really don't want to mix the copier module inside the src of the container, the reason being I feel the submodules to the main should be separated, yet inside the editor module.

Furthermore, I'm using go.mod as a way to get a clean image by compiling main.go and using the binary to create a new clean artifact, so I would like to have the go.mod and go.sum files inside editor/container/src/webCopy/

btw. I have checked the package names and everything is properly named.

答案1

得分: 1

如果您正在使用go modules构建 - 您不再使用GOPATH - 所以这不是问题所在。

如果您想要自定义构建,并且不必在docker构建中创建繁琐的git密钥访问存储库,您可以在go.mod中利用replace指令

所以在.../webCopy/go.mod中添加以下行:

replace bitbucket.org/backend/editor/copier => ../../../copier/

这将指示go build使用此相对路径(而不是直接的https下载)。

英文:

FYI if you are using a go modules build - you are no longer using GOPATH - so that is not the issue.

If you want a custom build - and not have to create laborious git key access to repo's from within a docker build - you can leverage the replace directive in go.mod

So add to .../webCopy/go.mod the following line:

replace bitbucket.org/backend/editor/copier => ../../../copier/

this will instruct the go build to use this relative path (instead of a direct https download)

huangapple
  • 本文由 发表于 2021年6月20日 04:55:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/68050669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定