亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當源代碼包含多個級別/目錄時部署 Google Cloud Function

當源代碼包含多個級別/目錄時部署 Google Cloud Function

Go
手掌心 2023-06-12 10:06:37
我想部署一個用 Go 編寫的 Google Cloud Function,其代碼結構包含一個子目錄,如下所示:function├── module1│   ├── go.mod│   └── module1.go├── go.mod└── entrypoint.go但是當我部署函數時,使用 GCP 控制臺或gcloud命令:# from function/ directorygcloud functions deploy myfunction --runtime go111 [...]僅go.mod和entrypoint.go已上傳(我檢查了GCP 控制臺中函數詳細信息的源選項卡)。因此該函數無法部署,因為顯然使用了.entrypoint.gomodule1/module1.go如果源是.zipGoogle Cloud Storage 上的一個(包含多個目錄),也會發生同樣的情況:gcloud functions deploy myfunction \    --runtime go111 \    --source gs://${BUCKET}/function.zip [...]是否可以使用帶有子目錄的代碼結構來部署函數?我不知道其他運行時(Python、NodeJS)是否也會發生同樣的情況,或者該問題是否特定于 Go。編輯我試著遵循這個指南:https://cloud.google.com/functions/docs/writing/#functions-writing-file-structuring-go(第二點:項目根目錄下的一個包,它從子包并導出一個或多個函數),如評論中所建議的那樣,但沒有成功。這是我使用的結構(在本地工作):.├── function.go├── go.mod└── shared    ├── go.mod    └── shared.gogo.modmodule testcloudfunctionrequire testcloudfunction/shared v0.0.0replace testcloudfunction/shared => ./shared函數.gopackage functionimport (    "fmt"    "testcloudfunction/shared")func HelloWorld(w http.ResponseWriter, r *http.Request) {    fmt.Fprint(w, shared.Hello())}共享/go.modmodule testcloudfunction/shared共享/shared.gopackage sharedfunc Hello() string {    return "Hello World!"}
查看完整描述

1 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

OK. It works for me with some changes.


I'm using GOPATH and so am prefixing with GO111MODULE=on. If you are outside of GOPATH, I think you can just drop the GO111MODULE=on environment setting.


Starting with directories and .go files only (no .mod files).


My path is github.com/DazWilkin/cloudfuncs.


IIUC you will need to -- minimally -- prefix module paths with example.com.


package function


import (

    "fmt"

    "net/http"

    "github.com/DazWilkin/cloudfuncs/shared"

)


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

    fmt.Fprint(w, shared.Hello())

}

Then, from within my cloudfuncs directory:


GO111MODULE=on go mod init github.com/DazWilkin/test

Results in a go.mod:


module github.com/DazWilkin/cloudfuncs


go 1.11

There is no go.mod file in .../cloudfuncs/shared.


Then I deploy using:


gcloud functions deploy HelloFreddie \

--region=us-central1 \

--entry-point=HelloFreddie \

--runtime=go111 \

--source=$PWD/cloudfuncs \

--project=${PROJECT} \

--trigger-http

You can see the result here: https://us-central1-dazwilkin-190225-54842789.cloudfunctions.net/HelloFreddie


查看完整回答
反對 回復 2023-06-12
  • 1 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號