目标:我想重用带有HTTP触发器的两个Go函数中的许多Go函数。
我尝试过的方法和重现该问题的步骤:
- 在GCP中,创建一个新的Go 1.11 Cloud Function HTTP触发器
- 命名为:
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/部分。
其他较小的问题: 我想重用的功能可能全部在同一个文件中,并且实际上不需要任何触发器,但是似乎没有触发器是不可能的。
我无法实现目标的相关问题和文档: