【问题标题】:Error when trying to deploy Google cloud function in Go 1.11 using go modules尝试使用 Go 模块在 Go 1.11 中部署 Google 云功能时出错
【发布时间】:2019-06-21 23:38:39
【问题描述】:

我在尝试使用 Go 模块在 Go 1.11 中部署 Google 云功能时遇到问题。我的GOPATH 中有以下代码结构:

└── example
    ├── models
    │   ├── go.mod
    │   └── models.go
    └── load
        ├── fn.go
        ├── go.mod
        ├── go.sum
        └── vendor
            └── ....

load/go.mod 如下所示:

module github.com/example/load

require (
    github.com/example/models v0.0.0
)

replace github.com/example/models => ../models

当我尝试使用命令部署函数时

gcloud functions deploy load-data --entry-point GCSNewFileTrigger --runtime go111 --trigger-resource new_data --trigger-event google.storage.object.finalize

我收到以下错误:

Deploying function (may take a while - up to 2 minutes)...failed.                                                                                                                                                                     
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: go: parsing /models/go.mod: open /models/go.mod: no such file or directory
go: error loading module requirements

命令go mod vendorgo mod verify在本地运行成功,我可以在load的vendor文件夹中看到我的本地包models

【问题讨论】:

    标签: go google-cloud-platform google-cloud-functions go-modules


    【解决方案1】:

    建造者更喜欢模块而不是供应商。如果有go.mod,将使用模块。当你上传你的函数时,它只包括你的函数在根目录,而不是任何上一级的目录。所以,当有go.mod 并且你有一个指向上一级的替换指令时,它将不起作用。

    解决方案是供应商而不是上传go.mod/go.sum 文件。使用gcloud 时,您可以创建一个.gcloudignore 文件来为您执行此操作。详情请参阅https://cloud.google.com/functions/docs/concepts/go-runtime#specifying_dependencies

    免责声明:我在 Google 工作并负责此产品。

    【讨论】:

    • metadata.FromContext 根据this post 使用 vendor 时会失败。没有供应商的任何解决方案?
    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多