Cloud Run functions で Go の使用をすぐに開始するには、クイックスタートをご覧ください。
ソースコードの構造
Cloud Run functions が関数の定義を見つけるには、ソースコードが特定の構造に従っている必要があります。詳細については、Cloud Run 関数の作成をご覧ください。
依存関係の指定
Go の Cloud Run functions では、Go モジュールと go.mod ファイル、または vendor ディレクトリのいずれかで、すべての依存関係を指定する必要があります。詳しくは、Go での依存関係の指定をご覧ください。
環境変数
Go ランタイムは、必要に応じて関数で使用する特定の環境変数を自動的に設定します。詳しくは、環境変数の使用をご覧ください。
Context 型
Go の context パッケージは Context 型を定義します。この型は、API の境界を越えてプロセス間で期限、キャンセル シグナル、その他のリクエスト スコープ値を保持します。
次の Cloud Run functions コードは、Pub/Sub クライアントによるコンテキスト アクセスの例を示しています。
// Package helloworld provides a set of Cloud Functions samples.packagehelloworldimport("context""fmt""log""github.com/GoogleCloudPlatform/functions-framework-go/functions""github.com/cloudevents/sdk-go/v2/event")funcinit(){functions.CloudEvent("HelloPubSub",helloPubSub)}// MessagePublishedData contains the full Pub/Sub message// See the documentation for more details:// https://cloud.google.com/eventarc/docs/cloudevents#pubsubtypeMessagePublishedDatastruct{MessagePubSubMessage}// PubSubMessage is the payload of a Pub/Sub event.// See the documentation for more details:// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessagetypePubSubMessagestruct{Data[]byte`json:"data"`}// helloPubSub consumes a CloudEvent message and extracts the Pub/Sub message.funchelloPubSub(ctxcontext.Context,eevent.Event)error{varmsgMessagePublishedDataiferr:=e.DataAs(&msg);err!=nil{returnfmt.Errorf("event.DataAs: %w",err)}name:=string(msg.Message.Data)// Automatically decoded from base64.ifname==""{name="World"}log.Printf("Hello, %s!",name)returnnil}
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-03-25 UTC。"],[[["Cloud Run functions utilize a runtime environment that includes the operating system, language support, and the Functions Framework library."],["You can choose a specific Go runtime version for your Cloud Run function during deployment, as listed in the Runtime support documentation."],["Dependencies for Go Cloud Run functions must be provided via Go modules with a `go.mod` file or a `vendor` directory."],["The source code for Cloud Run functions needs to adhere to a defined structure to ensure the function's definition can be located correctly."],["The `Context` type from Go's `context` package is used within Cloud Run functions for managing deadlines, cancellation signals, and request-scoped values, as shown in the Pub/Sub example."]]],[]]