Go 執行階段

Cloud Run 函式的執行環境包含作業系統版本、附加套件、語言支援,以及支援和叫用函式的 Go Functions Framework 程式庫。這個環境由語言版本識別,稱為執行階段 ID。

函式準備

您可以直接透過 Google Cloud 控制台準備函式,也可以在本機電腦上編寫函式後再上傳。如要準備本機電腦,用於 Go 開發作業,請參閱「設定 Go 開發環境」。

支援的 Go 執行階段和基本映像檔

執行階段 執行階段 ID 堆疊 執行階段基礎映像檔 淘汰 停用
Go 1.26 go126
  • google-24 (預設)
  • google-24-full
  • google-24/go126
  • google-24-full/go126
  • Go 1.25 go125
  • google-22 (預設)
  • google-22-full
  • google-22/go125
  • google-22-full/go125
  • Go 1.24 go124
  • google-22 (預設)
  • google-22-full
  • google-22/go124
  • google-22-full/go124
  • 2026-09-02 2027-03-02
    Go 1.23
    go123
  • google-22 (預設)
  • google-22-full
  • google-22/go123
  • google-22-full/go123
  • 2026-02-21 2026-08-21
    Go 1.22 go122
  • google-22 (預設)
  • google-22-full
  • google-22/go122
  • google-22-full/go122
  • 2026-01-28 2026-07-28
    Go 1.21 go121
  • google-22 (預設)
  • google-22-full
  • google-22/go121
  • google-22-full/go121
  • 2025-09-03 2026-03-03
    Go 1.20 go120
  • google-22 (預設)
  • google-22-full
  • google-22/go120
  • google-22-full/go120
  • 2024-05-01 2025-05-01
    Go 1.19 go119
  • google-22 (預設)
  • google-22-full
  • google-22/go119
  • google-22-full/go119
  • 2024-04-30 2025-01-30
    Go 1.18 go118
  • google-22 (預設)
  • google-22-full
  • google-22/go118
  • google-22-full/go120
  • 2024-01-30 2025-01-30
    Go 1.16 go116 google-18-full google-18-full/go116 2024-01-30 2025-01-30
    Go 1.13 go113 google-18-full google-18-full/go113 2024-01-30 2025-01-30
    Go 1.11 go111 已停用 已停用 2020-08-05 2021 年 2 月

    選取執行階段

    您可以在部署期間,為函式選取其中一個支援的 Go 執行階段。

    您可以使用 Google Cloud 控制台或 gcloud CLI 選取執行階段版本。按一下分頁標籤,瞭解如何使用自選工具:

    gcloud

    部署函式時,請使用 --base-image 旗標指定函式的 Go 基礎映像檔。例如:

    gcloud run deploy FUNCTION \
        --source . \
        --function FUNCTION_ENTRYPOINT \
        --base-image go126
    

    更改項目:

    • FUNCTION 替換為您要部署的函式名稱。您可以完全省略這個參數,但這樣系統會提示您輸入名稱。

    • FUNCTION_ENTRYPOINT,指定原始碼中函式的進入點。這是 Cloud Run 在函式執行時執行的程式碼。此旗標的值必須是原始碼中既有的函式名稱或完整類別名稱。

    如需使用 gcloud CLI 部署函式的詳細操作說明,請參閱「在 Cloud Run 中部署函式」。

    控制台

    在 Google Cloud 控制台中建立或更新 Cloud Run 函式時,可以選取執行階段版本。如需部署函式的詳細操作說明,請參閱「在 Cloud Run 中部署函式」。

    如要在 Google Cloud 控制台建立函式時選取執行階段,請按照下列步驟操作:

    1. 前往 Google Cloud 控制台的 Cloud Run 頁面:

      前往 Cloud Run

    2. 按一下「編寫函式」

    3. 在「Runtime」清單中,選取 Go 執行階段版本。

    4. 按一下「建立」,然後等待 Cloud Run 使用預留位置修訂版本建立服務。

    5. 控制台會將您重新導向至「來源」分頁,您可以在這裡查看函式的原始碼。按一下「儲存並重新部署」

    如需在部署函式後更新執行階段版本的詳細操作說明,請參閱「重新部署新的原始碼」。

    原始碼結構

    原始碼必須遵循特定結構,Cloud Run functions 才能找到函式的定義。詳情請參閱「編寫 Cloud Run 函式」。

    指定依附元件

    以 Go 編寫的 Cloud Run 函式必須透過含有 go.mod 檔案或 vendor 目錄的 Go 模組,提供所有依附元件。詳情請參閱「在 Go 中指定依附元件」。

    環境變數

    Go 執行階段會自動為函式設定特定環境變數,供函式視需要使用。詳情請參閱「設定環境變數」。

    Context」類型

    Go 的 context 套件定義了 Context 類型,可跨越 API 邊界,並在程序之間傳遞期限、取消信號和其他要求範圍的值。

    下列程式碼範例說明如何透過 Pub/Sub 用戶端存取背景資訊:

    
    // Package helloworld provides a set of Cloud Functions samples.
    package helloworld
    
    import (
    	"context"
    	"fmt"
    	"log"
    
    	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
    	"github.com/cloudevents/sdk-go/v2/event"
    )
    
    func init() {
    	functions.CloudEvent("HelloPubSub", helloPubSub)
    }
    
    // MessagePublishedData contains the full Pub/Sub message
    // See the documentation for more details:
    // https://cloud.google.com/eventarc/docs/cloudevents#pubsub
    type MessagePublishedData struct {
    	Message PubSubMessage
    }
    
    // PubSubMessage is the payload of a Pub/Sub event.
    // See the documentation for more details:
    // https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
    type PubSubMessage struct {
    	Data []byte `json:"data"`
    }
    
    // helloPubSub consumes a CloudEvent message and extracts the Pub/Sub message.
    func helloPubSub(ctx context.Context, e event.Event) error {
    	var msg MessagePublishedData
    	if err := e.DataAs(&msg); err != nil {
    		return fmt.Errorf("event.DataAs: %w", err)
    	}
    
    	name := string(msg.Message.Data) // Automatically decoded from base64.
    	if name == "" {
    		name = "World"
    	}
    	log.Printf("Hello, %s!", name)
    	return nil
    }