Go Quickstart

Google Cloud Functions is a serverless execution environment for building and connecting cloud services. With Cloud Functions you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services. Your function is triggered when an event being watched is fired.

This page shows you how to create and deploy a Go Cloud Function using the GCP Console. When this function is triggered by an HTTP request, it writes a message:

// Package p contains an HTTP Cloud Function.
package p

import (
  "encoding/json"
  "fmt"
  "html"
  "net/http"
)

// 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, "Hello World!")
    return
  }
  if d.Message == "" {
    fmt.Fprint(w, "Hello World!")
    return
  }
  fmt.Fprint(w, html.EscapeString(d.Message))
}

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Google Cloud Platform project.

    Learn how to enable billing

  4. Enable the Cloud Functions API.

    Enable the API

Create a function

  1. Open the Functions Overview page in the GCP Console:

    Go to the Cloud Functions Overview page

    Make sure that the project for which you enabled Cloud Functions is selected.

  2. Click Create function.

  3. Name your function.

  4. In the Trigger field, select HTTP.

  5. In the Source code field, select Inline editor. In this exercise, you will use the default function provided in the editor.

  6. Use the Runtime dropdown to select the desired Go runtime.

    Your display should resemble the following:

    Screenshot showing function creation form for Go

Deploy the function

  1. At the bottom of the page, click Create.

  2. After clicking Create, GCP Console redirects to the Cloud Functions Overview page.

    While the function is being deployed, the icon next to it is a small spinner. After it finishes deploying, the spinner turns to a green check mark:

    Screenshot that shows function deployment process

Test the function

  1. Display the menu for your function, and click Test function.

    Screenshot that shows function testing process

  2. On the testing page, click Test the function.

    The Output screen displays the text "Hello World!"

  3. Now change the message. In the Triggering Event field, enter the text {"message":"Hello, YOUR_NAME!"}, replacing YOUR_NAME with a name, and click Test the function.

    For example, suppose you entered the name "Rowan". In the Output field, you would see the message Hello, Rowan!.

    In the Logs field, a status code of 200 indicates success.

    Screenshot showing Logs field with 200 code, indicating success

View logs

Check the logs to see your actions in the log history:

  • Back on the Cloud Functions Overview page, display the menu for your function, and click View logs.

Your log history appears.

Screenshot showing Logs field with 200 code, indicating success

What's next

Var denne siden nyttig? Si fra hva du synes:

Send tilbakemelding om ...

Cloud Functions Documentation