如何在本地安装作为 github.com 地址引用的 Golang 软件包



我有一个包,我在 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

我收到此异常消息:

---> 在 bb3ec43f7fea 中运行 examples/go-examples/exclamationFunc.go:24:2: 找不到包 "github.com/apache/pulsar/pulsar-function-go/pf"在以下任何一项中:/usr/local/go/src/github.com/apache/pulsar/pulsar-function-go/pf (from from $GOROOT(/go/src/github.com/apache/pulsar/pulsar-function-go/pf (来自$GOPATH(

有没有办法允许我使用同一包的本地安装版本,而无需更改我尝试生成的代码中的引用?

编辑这是我尝试过的其他一切:

当我在脉冲星函数中运行go安装时,我得到:

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
)

从下面的答案中,我被告知我可以在 Go 模块中插入一个replace指令,以允许我们在为测试构建 Go 二进制文件时指向本地文件。 但是,我认为我无法正确安装模块,可能是由于模块中包含脉冲星函数/示例的方式。 因此,我无法弄清楚要将其设置为/local/path/to/package的内容,因为我无法成功安装软件包。(也许我也对此感到困惑?

有没有更好的方法来构建模块/代码,以便将本地脉冲星函数-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

相关内容

最新更新