首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在本地安装作为github.com地址引用的Golang包

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

Stack Overflow用户
提问于 2020-01-23 01:26:40
回答 1查看 841关注 0票数 0

我在Go代码中引用了一个包为:

代码语言:javascript
复制
github.com/apache/pulsar/pulsar-function-go/pf

但是,在构建用于设置集成测试的坞映像时,我需要包括对该库的一些本地更改。

在我的Dockerfile中,我设置了GOPATH和GOROOT,路径如下:

代码语言:javascript
复制
ENV GOROOT /usr/local/go
ENV GOPATH /go
ENV PATH /go/bin:$PATH

然后,在Dockerfile中,我将源代码复制到Docker映像中,并尝试安装Go模块,如下所示:

代码语言:javascript
复制
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

我收到这条异常信息:

运行在"github.com/apache/pulsar/pulsar-function-go/pf“示例中的

--运行于bb3ec43f7fea示例/go-示例/惊叹Func.go:24:2:无法在以下任何一个中找到包:/usr/local/go/src/github.com/apache/pulsar/pulsar-function-go/pf (来自$GOROOT) /go/src/github.com/apache/pulsar/pulsar-function-go/pf (来自$GOPATH)

是否有一种方法可以允许我使用该包的本地安装版本,而不需要更改我试图构建的代码中的引用?

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

当我运行go安装在脉冲星-function,我得到:

代码语言:javascript
复制
cannot find module for path .

当我在脉冲星-function/pf中运行go安装时,我得到:

代码语言:javascript
复制
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)

当我尝试跑步时:

代码语言:javascript
复制
RUN go get -t github.com/apache/pulsar/pulsar-function-go/...

(虽然这不会带来我的本地更改,但只是为了测试)我得到:

代码语言:javascript
复制
# 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

当我尝试跑步时:

代码语言:javascript
复制
RUN go get -t github.com/apache/pulsar/pulsar-function-go

我得到:

代码语言:javascript
复制
package github.com/apache/pulsar/pulsar-function-go: no Go files in /go/src/github.com/apache/pulsar/pulsar-function-go

如果我尝试运行这个来首先获得一些依赖项:

代码语言:javascript
复制
RUN go get -t github.com/apache/pulsar/pulsar-function-go/pf

它成功了。如果我在Dockerfile中这样做的话:

代码语言:javascript
复制
# 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

要用本地代码替换pulsar-function文件的源代码,那么当我试图构建二进制文件时,如下所示:

代码语言:javascript
复制
RUN go build -o /pulsar/examples/go-examples/exclamationFunc /pulsar/examples/go-examples/exclamationFunc.go

我得到:

代码语言:javascript
复制
/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文件中,如下所示:

代码语言:javascript
复制
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二进制文件时能够指向本地文件。然而,我认为我不能正确地安装模块,可能是由于脉冲星函数go/示例包含在模块中的方式。因此,我无法确定将什么设置为/local/path/to/package,因为我无法成功地安装软件包。(也许我也对此感到困惑?)

是否有更好的方法来构造模块/代码,以便能够在测试码头映像中安装本地脉冲星功能-go模块?

如果您想自己查看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

EN

回答 1

Stack Overflow用户

发布于 2020-01-23 02:05:04

为您的项目使用go模块,并在其中包含一个针对您所引用的包的replace指令:

代码语言:javascript
复制
replace github.com/apache/pulsar/pulsar-function-go/pf => /local/path/to/package
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59870466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档