Google Cloud 콘솔에서 직접 함수를 준비하거나 로컬 머신에서 작성 후 업로드할 수 있습니다. Go 개발용 로컬 머신을 준비하려면 Go 개발 환경 설정을 참조하세요.
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."]]],[]]