How to call a Go Cloud Function from another Go Cloud Function in GCP

回眸只為那壹抹淺笑 提交于 2020-01-05 05:54:28

问题


Goal: I want to reuse many Go functions from two Go functions with HTTP triggers.

What I have tried and steps to reproduce the problem:

  1. In GCP, create a new Go 1.11 Cloud Function, HTTP Trigger
  2. Name it: MyReusableHelloWorld
  3. In function.go, paste this:
package Potatoes

import (   
    "net/http"
)


// Potatoes return potatoes
func Potatoes(http.ResponseWriter, *http.Request) {

}
  1. In go.mod, paste this: module example.com/foo
  2. In function to execute, paste this: Potatoes
  3. Click on deploy. It works.
  4. Create another Go serverless function in GCP
  5. In function. go, paste this:
// 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))
}
  1. In go.mod, paste this: module example.com/foo
  2. In function to execute, paste this: HelloWorld
  3. Click on deploy. It doesn't work. You have the error: unknown import path "example.com/foo/Potatoes": cannot find module providing package example.com/foo/Potatoes

I have also tried all kinds of combinations for the module/packages to import. I have tried without the example.com/ part.

Other smaller issue: The functions I want to reuse could all be in the same file and don't really need any trigger, but it doesn't seem that having no trigger is possible.

Related questions and documentation with which I could not achieve my goal:

  1. How can I use a sub-packages with Go on Google Cloud Functions?
  2. https://github.com/golang/go/wiki/Modules , section go.mod

回答1:


You can’t invoke a cloud function from another one, because each function is in its own container independently.

So If you want to deploy the function with a dependency that can't be downloaded from a package manager you need to put the code together like here and deploy using the CLI




回答2:


It is likely each cloud function defined in the console is independent of the other. If you want code reuse, it's best to structure it as per the following document and deploy it using gcloud command.

https://cloud.google.com/functions/docs/writing/#structuring_source_code




回答3:


You are mixing things: package management and function deployment.

When you deploy a Cloud Function, if you want to (re)use it, you have to call if with http package.

If you build a package that you want to include in your source code, you have to rely on package manager. With Go, Git repository, like Github, is the best way to achieve this (Don't forget to perform a release and to name it as expected by Go mod: vX.Y.Z)

Here your code can't work without more engineering and package publication/management.

I achieve the same things but with a Dockerfile and I deplored my code in Cloud Run (that I recommend you if you aren't event oriented and only HTTP oriented. I wrote a comparison on Medium)

  • Root
    • go.mod
    • pkg/foo.go
    • pkg/go.mod
    • service/Helloworld.go
    • service/go.mod

In my helloworld.go, I can reuse the package foo. For this I perform this in my service/go.mod file

module service/helloworld

go 1.12

require pkg/foo v0.0.0

replace pkg/foo v0.0.0 => ../pkg

Then when you build your container, you run your go build service/Helloworld.go from the root directory.



来源:https://stackoverflow.com/questions/58722009/how-to-call-a-go-cloud-function-from-another-go-cloud-function-in-gcp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!