尝试使用go模块在Go 1.11中部署Google云功能时出错

sam*_*awy 6 go google-cloud-platform google-cloud-functions go-modules

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

??? example
    ??? models
    ?   ??? go.mod
    ?   ??? models.go
    ??? load
        ??? fn.go
        ??? go.mod
        ??? go.sum
        ??? vendor
            ??? ....
Run Code Online (Sandbox Code Playgroud)

load/go.mod如下所示:

module github.com/example/load

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

replace github.com/example/models => ../models
Run Code Online (Sandbox Code Playgroud)

当我尝试使用该命令部署该功能时

gcloud functions deploy load-data --entry-point GCSNewFileTrigger --runtime go111 --trigger-resource new_data --trigger-event google.storage.object.finalize
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

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

Tyl*_*ich 6

建造者比供应商更喜欢模块。如果有go.mod,将使用模块。当您上传您的函数时,它仅包含您的函数在根目录下的目录,而不包含任何上一级的目录。因此,当有 ago.mod并且您有一个指向上一层的替换指令时,它将不起作用。

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

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