尝试使用谷歌功能与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
型
1条答案
按热度按时间2vuwiymt1#
感谢@erik258,我能够找到我问题的答案。答案没有直接在您链接的文档中找到,但我在这里找到了:Write Cloud Functions。有一个关于项目结构组织的特定部分。我的错误是,我最初将项目设置为常规GO项目,并将
main.go
放在根文件夹中。将文件移动到建议的\cmd
后,文件夹并重新排列main.go
中的导入,它开始工作了!感谢您发送编修。