How do I invoke serverless functions on GCP using Go?

Invoking serverless functions on Google Cloud Platform (GCP) using Go is a straightforward process. Google Cloud Functions allows you to run your code in response to events and is ideal for building serverless applications. Below is a simple example of how to invoke a Cloud Function using Go.

package main import ( "context" "fmt" "net/http" "log" "cloud.google.com/go/functions/metadata" ) func invokeFunction(ctx context.Context, functionName string) { url := fmt.Sprintf("https://REGION-PROJECT_ID.cloudfunctions.net/%s", functionName) req, err := http.NewRequest("GET", url, nil) if err != nil { log.Fatalf("Failed to create request: %v", err) } client := &http.Client{} resp, err := client.Do(req) if err != nil { log.Fatalf("Failed to invoke function: %v", err) } defer resp.Body.Close() if resp.StatusCode == http.StatusOK { log.Println("Function invoked successfully") } else { log.Printf("Function invocation failed: %s", resp.Status) } } func main() { ctx := context.Background() invokeFunction(ctx, "myFunction") }

serverless functions GCP Google Cloud Platform invoke Cloud Functions Go programming serverless applications