如何从 GCP 中的另一个 Go Cloud Function 调用 Go Cloud Function
时间:2023-03-07
浏览:170
作者:佚名
哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《如何从 GCP 中的另一个 Go Cloud Function 调用 Go Cloud Function》,本文主要会讲到golang等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!
问题内容
目标: 我想通过 HTTP 触发器重用两个 Go 函数中的许多 Go 函数。
我尝试过的方法和重现问题的步骤:
- 在 GCP 中,新建一个 Go 1.11 Cloud Function,HTTP Trigger
- 命名它:
MyReusableHelloWorld - 在
function.go中,粘贴:
package Potatoes
import (
"net/http"
)
// Potatoes return potatoes
func Potatoes(http.ResponseWriter, *http.Request) {
}
- 在
go.mod中,粘贴:module example.com/foo - 在要执行的函数中,粘贴:
Potatoes - 点击部署。有用。
- 在 GCP 中创建另一个 Go 无服务器函数
- 在功能上。去,粘贴这个:
// Package p contains an HTTP Cloud Function.
package p
import (
"encoding/json"
"fmt"
"html"
"net/http"
"example.com/foo/Potatoes"
)
// HelloWorld prints the JSON encoded "message" field in the body
// of the request or "Hello, World!" if there isn't one.
func HelloWorld(w http.ResponseWriter, r *http.Request) {
var d struct {
Message string `json:"message"`
}
if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
fmt.Fprint(w, "error here!")
return
}
if d.Message == "" {
fmt.Fprint(w, "oh boy Hello World!")
return
}
fmt.Fprint(w, html.EscapeString(d.Message))
}
- 在
go.mod中,粘贴:module example.com/foo - 在要执行的函数中,粘贴:
HelloWorld - 点击部署。 它不起作用。 你有错误:
unknown import path "example.com/foo/Potatoes": cannot find module providing package example.com/foo/Potatoes
我还尝试了各种组合来导入模块/包。我试过没有 example.com/ 部分。
其他较小的问题: 我想重用的函数可能都在同一个文件中,并且不需要任何触发器,但似乎没有触发器是不可能的。
我无法实现目标的相关问题和文档:
- 如何在 Go on Google Cloud Functions 上使用子包?
- https://github.com/golang/go/wiki/Modules,部分 go.mod
正确答案
您可以从另一个云函数调用云函数,因为每个函数都独立地位于其自己的容器中。
因此,如果您想部署具有无法从包管理器下载的依赖项的函数,您需要像[这里一样将代码放在一起并使用](https://cloud.google.com/functions/docs/writing/#functions- writing-file-structuring- nodejs)CLI进行部署
以上就是《如何从 GCP 中的另一个 Go Cloud Function 调用 Go Cloud Function》的详细内容,更多关于golang的资料请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.28
热门专题
