无法从GO源代码构建Google Function

pbwdgjma  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(126)

尝试使用谷歌功能与GO语言和一切工作正常本地-我可以运行的功能,一切似乎都在工作
一旦我从命令行部署函数,我在Google Build上收到一个错误:

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed with status: FAILURE and message: functions.local/app/main.go:18:2: import "github.com/my/app" is a program, not an importable package. For more details see the logs at https://console.cloud.google.com/cloud-build/builds;region=....

字符串
我用的是go 1.21.4
我有一个函数包含这样的文件:

package handlers

import (
     ...
     ...
    "github.com/my/app/data"
    "github.com/my/app/utils"
    "github.com/GoogleCloudPlatform/functions-framework-go/functions"
)

func init() {
    functions.HTTP("handleFunc", IncomingCall)
}

func IncomingCall(w http.ResponseWriter, r *http.Request) {
...
...
}


实际上是main.go

package main

import (
    "fmt"
    "log"
    "os"

    // Blank-import the function package so the init() runs
    _ "github.com/my/app/handlers"
    "github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
)

const DEFAULT_PORT = 12789

func main() {
    fmt.Println("Started")

    // logger := utils.CreateLogger()

    port := os.Getenv("PORT")
    if len(port) == 0 {
        port = string(DEFAULT_PORT)
    }

    if err := funcframework.Start(port); err != nil {
        log.Fatalf("funcframework.Start: %v\n", err)
    }

    log.Println("Done")
}


什么可能是一个程序?我如何修复此错误:
import "github.com/my/app" is a program, not an importable package"
go.mod

module github.com/my/app

go 1.21.4

require github.com/GoogleCloudPlatform/functions-framework-go v1.8.0

require (
    cloud.google.com/go/functions v1.15.1 // indirect
    github.com/cloudevents/sdk-go/v2 v2.14.0 // indirect
    github.com/google/uuid v1.3.0 // indirect
    github.com/json-iterator/go v1.1.10 // indirect
    github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
    github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
    go.uber.org/multierr v1.10.0 // indirect
    go.uber.org/zap v1.26.0 // indirect
)


deploy命令:

gcloud functions deploy temp01 `
    --gen2 `
    --runtime=go121 `
    --region=europe-west6 `
    --source=. `
    --entry-point handleFunc `
    --trigger-http `
    --allow-unauthenticated

2vuwiymt

2vuwiymt1#

感谢@erik258,我能够找到我问题的答案。答案没有直接在您链接的文档中找到,但我在这里找到了:Write Cloud Functions。有一个关于项目结构组织的特定部分。我的错误是,我最初将项目设置为常规GO项目,并将main.go放在根文件夹中。将文件移动到建议的\cmd后,文件夹并重新排列main.go中的导入,它开始工作了!
感谢您发送编修。

相关问题