我有一个包,我在我的 Go 代码中引用为:
github.com/apache/pulsar/pulsar-function-go/pf
但是,在构建 docker 镜像以设置集成测试时,我需要对该库进行一些本地更改。
在我的 Dockerfile 中,我设置了 GOPATH 和 GOROOT 和 PATH,如下所示:
ENV GOROOT /usr/local/go
ENV GOPATH /go
ENV PATH /go/bin:$PATH
然后,在 Dockerfile 中,我将源代码复制到我的 Docker 镜像中并尝试安装 Go 模块,如下所示:
COPY target/pulsar-function-go/ /pulsar/dependencies/pulsar-function-go/
RUN cd /pulsar/dependencies/pulsar-function-go/pf && go install .
但是,当我尝试构建一个引用
github.com/apache/pulsar/pulsar-function-go/pf
的文件时通过运行这一行:RUN go build -o /pulsar/examples/go-examples/exclamationFunc /pulsar/examples/go-examples/exclamationFunc.go
我收到此异常消息:
---> Running in bb3ec43f7fea examples/go-examples/exclamationFunc.go:24:2: cannot find package "github.com/apache/pulsar/pulsar-function-go/pf" in any of: /usr/local/go/src/github.com/apache/pulsar/pulsar-function-go/pf (from $GOROOT) /go/src/github.com/apache/pulsar/pulsar-function-go/pf (from $GOPATH)
有没有办法让我使用同一个包的本地安装版本,而无需更改我正在尝试构建的代码中的引用?
编辑这是我尝试过的所有其他内容:
当我在 pulsar-function-go 中运行 go install 时,我得到:
cannot find module for path .
当我在 pulsar-function-go/pf 中运行 go install 时,我得到:
examples/go-examples/exclamationFunc.go:24:2: cannot find package "github.com/apache/pulsar/pulsar-function-go/pf" in any of:
/usr/local/go/src/github.com/apache/pulsar/pulsar-function-go/pf (from $GOROOT)
/go/src/github.com/apache/pulsar/pulsar-function-go/pf (from $GOPATH)
当我尝试运行时:
RUN go get -t github.com/apache/pulsar/pulsar-function-go/...
(虽然这不会带来我的本地更改,但这只是为了测试)
我得到:
# github.com/apache/pulsar/pulsar-function-go/examples/test
/go/src/github.com/apache/pulsar/pulsar-function-go/examples/test/producer.go:30:6: main redeclared in this block
previous declaration at /go/src/github.com/apache/pulsar/pulsar-function-go/examples/test/consumer.go:30:6
# github.com/apache/pulsar/pulsar-function-go/examples
/go/src/github.com/apache/pulsar/pulsar-function-go/examples/hello.go:32:6: main redeclared in this block
previous declaration at /go/src/github.com/apache/pulsar/pulsar-function-go/examples/contextFunc.go:36:6
/go/src/github.com/apache/pulsar/pulsar-function-go/examples/inputFunc.go:34:6: main redeclared in this block
previous declaration at /go/src/github.com/apache/pulsar/pulsar-function-go/examples/hello.go:32:6
/go/src/github.com/apache/pulsar/pulsar-function-go/examples/outputFunc.go:33:6: main redeclared in this block
previous declaration at /go/src/github.com/apache/pulsar/pulsar-function-go/examples/inputFunc.go:34:6
当我尝试运行时:
RUN go get -t github.com/apache/pulsar/pulsar-function-go
我得到:
package github.com/apache/pulsar/pulsar-function-go: no Go files in /go/src/github.com/apache/pulsar/pulsar-function-go
如果我尝试运行它以首先获取一些依赖项:
RUN go get -t github.com/apache/pulsar/pulsar-function-go/pf
它成功了。如果我在我的 Dockerfile 中遵循以下内容:
# after using Maven plugin to copy files from the pulsar-function-go source directory to target/pulsar-function-go
COPY target/pulsar-function-go/ /go/src/github.com/apache/pulsar/pulsar-function-go
RUN cd /go/src/github.com/apache/pulsar/pulsar-function-go/pf && go install
用我的本地代码替换 docker 文件中 pulsar-function-go 的源代码,然后当我尝试构建二进制文件时,如下所示:
RUN go build -o /pulsar/examples/go-examples/exclamationFunc /pulsar/examples/go-examples/exclamationFunc.go
我得到:
/go/src/github.com/apache/pulsar/pulsar-function-go/pb/InstanceCommunication.pb.go:29:2: cannot find package "google.golang.org/grpc" in any of:
/usr/local/go/src/google.golang.org/grpc (from $GOROOT)
/go/src/google.golang.org/grpc (from $GOPATH)
/go/src/github.com/apache/pulsar/pulsar-function-go/pb/InstanceCommunication.pb.go:30:2: cannot find package "google.golang.org/grpc/codes" in any of:
/usr/local/go/src/google.golang.org/grpc/codes (from $GOROOT)
/go/src/google.golang.org/grpc/codes (from $GOPATH)
/go/src/github.com/apache/pulsar/pulsar-function-go/pb/InstanceCommunication.pb.go:31:2: cannot find package "google.golang.org/grpc/status" in any of:
/usr/local/go/src/google.golang.org/grpc/status (from $GOROOT)
/go/src/google.golang.org/grpc/status (from $GOPATH)
12:13
google.golang.org/grpc 包包含在
go.mod
中文件,如下所示:module github.com/apache/pulsar/pulsar-function-go
go 1.13
require (
github.com/apache/pulsar-client-go v0.0.0-20200116214305-4d788d9935ed
github.com/golang/protobuf v1.3.2
github.com/sirupsen/logrus v1.4.1
github.com/stretchr/testify v1.3.0
golang.org/x/tools v0.0.0-20200119215504-eb0d8dd85bcc // indirect
google.golang.org/grpc v1.26.0
gopkg.in/yaml.v2 v2.2.2
)
从下面的答案中,我被告知我可以插入
replace
Go 模块中的指令允许我们在为测试构建 Go 二进制文件时指向本地文件。但是,我认为我无法正确安装模块,这可能是由于模块中包含 pulsar-function-go/examples 的方式。
所以,我不知道设置为
/local/path/to/package
因为我无法成功安装软件包。 (也许我也对此感到困惑?)有没有更好的方法来构建模块/代码,以便将本地 pulsar-function-go 模块安装到测试 Docker 镜像中?
如果您想自己查看 Go 代码,请在此处查看:https://github.com/apache/pulsar/tree/master/pulsar-function-go
Dockerfile 在这里:https://github.com/apache/pulsar/blob/master/tests/docker-images/latest-version-image/Dockerfile
最佳答案
为您的项目使用 go 模块,并包含 replace
您要引用的包的指令:
replace github.com/apache/pulsar/pulsar-function-go/pf => /local/path/to/package
关于docker - 如何在本地安装引用为 github.com 地址的 Golang 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59870466/