{"id":15221840,"url":"https://github.com/googlecloudplatform/functions-framework-go","last_synced_at":"2025-05-14T20:10:13.178Z","repository":{"id":37513424,"uuid":"215139378","full_name":"GoogleCloudPlatform/functions-framework-go","owner":"GoogleCloudPlatform","description":"FaaS (Function as a service) framework for writing portable Go functions","archived":false,"fork":false,"pushed_at":"2025-03-27T17:50:35.000Z","size":6656,"stargazers_count":496,"open_issues_count":9,"forks_count":63,"subscribers_count":37,"default_branch":"main","last_synced_at":"2025-04-13T17:46:43.366Z","etag":null,"topics":["cloud-functions","functions-as-a-service","golang","google-cloud"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/GoogleCloudPlatform/functions-framework-go","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GoogleCloudPlatform.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-14T20:30:34.000Z","updated_at":"2025-04-10T08:33:58.000Z","dependencies_parsed_at":"2023-09-27T01:27:37.884Z","dependency_job_id":"1ec72f9a-2022-4155-bb33-148f17c07f1a","html_url":"https://github.com/GoogleCloudPlatform/functions-framework-go","commit_stats":{"total_commits":149,"total_committers":24,"mean_commits":6.208333333333333,"dds":0.7986577181208054,"last_synced_commit":"93154d3fefe4e5d838b78d01479ff3b0bbeec4e6"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GoogleCloudPlatform","download_url":"https://codeload.github.com/GoogleCloudPlatform/functions-framework-go/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254219374,"owners_count":22034397,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloud-functions","functions-as-a-service","golang","google-cloud"],"created_at":"2024-09-28T15:08:07.849Z","updated_at":"2025-05-14T20:10:13.159Z","avatar_url":"https://github.com/GoogleCloudPlatform.png","language":"Go","readme":"# Functions Framework for Go\n\n[![GoDoc](https://godoc.org/github.com/GoogleCloudPlatform/functions-framework-go?status.svg)](http://godoc.org/github.com/GoogleCloudPlatform/functions-framework-go) [![Go version](https://img.shields.io/badge/go-v1.18+-blue)](https://golang.org/dl/#stable)\n\n[![Go unit CI][ff_go_unit_img]][ff_go_unit_link] [![Go lint CI][ff_go_lint_img]][ff_go_lint_link] [![Go conformace CI][ff_go_conformance_img]][ff_go_conformance_link]  ![Security Scorecard](https://api.securityscorecards.dev/projects/github.com/GoogleCloudPlatform/functions-framework-go/badge)\n\nAn open source FaaS (Function as a Service) framework for writing portable\nGo functions.\n\nThe Functions Framework lets you write lightweight functions that run in many\ndifferent environments, including:\n\n*   [Google Cloud Run functions](https://cloud.google.com/functions/)\n*   Your local development machine\n*   [Knative](https://github.com/knative/)-based environments\n*   [Google App Engine](https://cloud.google.com/appengine/docs/go/)\n*   [Google Cloud Run](https://cloud.google.com/run/docs/quickstarts/build-and-deploy)\n\nThe framework allows you to go from:\n\n```golang\nfunc HelloWorld(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprint(w, \"Hello, World!\")\n}\n```\n\nTo:\n\n```sh\ncurl http://my-url\n# Output: Hello, World!\n```\n\nAll without needing to worry about writing an HTTP server or request\nhandling logic.\n\n## Features\n\n*   Build your Function in the same container environment used by Cloud Run functions using [buildpacks](https://github.com/GoogleCloudPlatform/buildpacks).\n*   Invoke a function in response to a request\n*   Automatically unmarshal events conforming to the\n    [CloudEvents](https://cloudevents.io/) spec\n*   Portable between serverless platforms\n\n## Quickstart: Hello, World on your local machine\n\n1. Install Go 1.18+.\n\n1. Create a Go module:\n\t```sh\n\tgo mod init example.com/hello\n\t```\n\n\t\u003e Note: You can use a different module name rather than `example.com/hello`.\n\n1. Create a `function.go` file with the following contents:\n\t```golang\n\tpackage function\n\n\timport (\n\t\t\"fmt\"\n\t\t\"net/http\"\n\n\t\t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n\t)\n\n\tfunc init() {\n\t\tfunctions.HTTP(\"HelloWorld\", helloWorld)\n\t}\n\n\t// helloWorld writes \"Hello, World!\" to the HTTP response.\n\tfunc helloWorld(w http.ResponseWriter, r *http.Request) {\n\t\tfmt.Fprintln(w, \"Hello, World!\")\n\t}\n\t```\n\n\t\u003e Note that you can use any file name or package name (convention is to make\n\tpackage name same as directory name).\n\n1. To run locally, you'll need to create a main package to start your server\n   (see instructions below for container builds to skip this step and match your\n   local development environment to production):\n   ```sh\n   mkdir cmd\n   ```\n\n1. Create a `cmd/main.go` file with the following contents:\n\t```golang\n\tpackage main\n\n\timport (\n\t\t\"log\"\n\t\t\"os\"\n\n\t\t// Blank-import the function package so the init() runs\n\t\t_ \"example.com/hello\"\n\t\t\"github.com/GoogleCloudPlatform/functions-framework-go/funcframework\"\n\t)\n\n\tfunc main() {\n\t\t// Use PORT environment variable, or default to 8080.\n\t\tport := \"8080\"\n\t\tif envPort := os.Getenv(\"PORT\"); envPort != \"\" {\n\t\t\tport = envPort\n\t\t}\n\t\t\n\t\t// By default, listen on all interfaces. If testing locally, run with \n\t\t// LOCAL_ONLY=true to avoid triggering firewall warnings and \n\t\t// exposing the server outside of your own machine.\n\t\thostname := \"\"\n\t\tif localOnly := os.Getenv(\"LOCAL_ONLY\"); localOnly == \"true\" {\n\t\t\thostname = \"127.0.0.1\"\n\t\t} \n\t\tif err := funcframework.StartHostPort(hostname, port); err != nil {\n\t\t\tlog.Fatalf(\"funcframework.StartHostPort: %v\\n\", err)\n\t\t}\n\t}\n\t```\n\n1. Run `go mod tidy` to update dependency requirements.\n\n1. Start the local development server:\n\t```sh\n\tFUNCTION_TARGET=HelloWorld LOCAL_ONLY=true go run cmd/main.go\n\t# Output: Serving function: HelloWorld\n\t```\n\n\tUpon starting, the framework will listen to HTTP requests at `/` and invoke your registered function\n\tspecified by the `FUNCTION_TARGET` environment variable (i.e. `FUNCTION_TARGET=HelloWorld`).\n\n1. Send requests to this function using `curl` from another terminal window:\n\t```sh\n\tcurl localhost:8080\n\t# Output: Hello, World!\n\t```\n## Quickstart: Enable Exeuction Id Logging\n\n[Cloud Run Functions(1st gen)](https://cloud.google.com/functions/1stgendocs/deploy) provides an execution id in the logs at `labels.execution_id`, which customers can use to filter their logs for each execution. [Cloud Run Functions](https://cloud.google.com/functions/docs/deploy) doesn't have the same feature embedded. \n\nTo have exeuction id logged for `Cloud Run Functions` executions, users can either:\n\n* Provide a custom execution Id in the Http Header `Function-Execution-Id`.\n\n\t```sh\n\t\tcurl -H \"Function-Execution-Id: 123456\" localhost:8080\n\t\t# Output: Hello, World!\n\t```\n\n\tExample Log:\n\t```\n\t{\"message\":\"Try logging with executionID!\",\"logging.googleapis.com/labels\":{\"execution_id\":\"123456\"}}\n\t```\n\n\nOR \n* Leverage `LogWriter` provided in function-framework-go(v1.9.0 or higher) library to generate logs. If `Function-Exeuction-Id` is empty, a pseduorandom execution id will be auto-generated if `LogWriter` is used.  \n\n\t```golang\n\tpackage function\n\n\timport (\n\t\t\"fmt\"\n\t\t\"net/http\"\n\t\t\"log\"\n\t\t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n\t\t\"github.com/GoogleCloudPlatform/functions-framework-go/funcframework\"\n\t)\n\n\tfunc init() {\n\t\tfunctions.HTTP(\"HelloWorld\", helloWorld)\n\t}\n\n\t// helloWorld writes \"Hello, World!\" to the HTTP response.\n\tfunc helloWorld(w http.ResponseWriter, r *http.Request) {\n\t\tl := log.New(funcframework.LogWriter(r.Context()), \"\", 0)\n\n\t\tl.Println(\"Try logging with executionID!\")\n\t\tfmt.Fprintln(w, \"Hello, World!\")\n\t}\n\t```\n\n\tExample Log:\n\t```\n\t{\"message\":\"Try logging with executionID!\",\"logging.googleapis.com/labels\":{\"execution_id\":\"181dbb5b096549313d470dd68fa64d96\"}}\n\t```\n\n\n## Go further: build a deployable container\n\n1. Install [Docker](https://store.docker.com/search?type=edition\u0026offering=community) and the [`pack` tool](https://buildpacks.io/docs/install-pack/).\n\n1. Build a container from your function using the Functions [buildpacks](https://github.com/GoogleCloudPlatform/buildpacks):\n\t```sh\n\tpack build \\\n\t\t--builder gcr.io/buildpacks/builder:v1 \\\n\t\t--env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \\\n\t\t--env GOOGLE_FUNCTION_TARGET=HelloWorld \\\n\t\tmy-first-function\n\t```\n\n1. Start the built container:\n\t```sh\n\tdocker run --rm -p 8080:8080 my-first-function\n\t# Output: Serving function...\n\t```\n\n1. Send requests to this function using `curl` from another terminal window:\n\t```sh\n\tcurl localhost:8080\n\t# Output: Hello, World!\n\t```\n\n## Run your function on serverless platforms\n\n### Google Cloud Run functions\n\nDeploy from your local machine using the `gcloud` command-line tool.\n[Check out the Cloud Functions quickstart](https://cloud.google.com/functions/docs/quickstart).\n\n### Container environments based on Knative\n\nThe Functions Framework is designed to be compatible with Knative environments.\nJust build and deploy your container to a Knative environment. Note that your app needs to listen\n`PORT` environment variable per [Knative runtime contract](https://github.com/knative/serving/blob/main/docs/runtime-contract.md#inbound-network-connectivity).\n\n## Functions Framework Features\n\nThe Go Functions Framework conforms to the [Functions Framework Contract](https://github.com/GoogleCloudPlatform/functions-framework), As such, it\nsupports HTTP functions, background event functions, and CloudEvent functions\n(as of v1.1.0). The primary build mechanism is the [GCP buildpacks stack](https://github.com/GoogleCloudPlatform/buildpacks), which takes a function of\none of the accepted types, converts it to a full HTTP serving app, and creates a\nlaunchable container to run the server.\n\n### HTTP Functions\n\nThe Framework provides support for handling native Go HTTP-style functions:\n\n```golang\npackage function\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n)\n\nfunc init() {\n\tfunctions.HTTP(\"HelloWorld\", helloWorld)\n}\n\nfunc helloWorld(w http.ResponseWriter, r *http.Request) {\n\tw.Write([]byte(\"Hello, World!\"))\n}\n```\n\n### CloudEvent Functions\n\nThe Functions Framework provides support for unmarshalling an incoming\n[CloudEvent](https://cloudevents.io/) payload into a `cloudevents.Event` object.\nThese will be passed as arguments to your function when it receives a request.\n\n```golang\npackage function\n\nimport (\n\tcloudevents \"github.com/cloudevents/sdk-go/v2\"\n\t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n)\n\nfunc init() {\n\tfunctions.CloudEvent(\"CloudEventFunc\", cloudEventFunc)\n}\n\nfunc cloudEventFunc(ctx context.Context, e cloudevents.Event) error {\n\t// Do something with event.Context and event.Data (via event.DataAs(foo)).\n\treturn nil\n}\n```\n\nThese functions are registered with the handler via `funcframework.RegisterCloudEventFunctionContext`.\n\nTo learn more about CloudEvents, see the [Go SDK for CloudEvents](https://github.com/cloudevents/sdk-go).\n\n### Background Event Functions\n\n[Background events](https://cloud.google.com/functions/docs/writing/background)\nare also supported. This type of function takes two parameters: a Go context and\na user-defined data struct.\n\n```golang\nfunc BackgroundEventFunction(ctx context.Context, data userDefinedEventStruct) error {\n\t// Do something with ctx and data.\n}\n```\n\nThis type of event requires you to define a struct with the\nappropriate data fields (e.g. those for a PubSub message or GCS event) and pass\nthat struct as the data parameter. See the [samples](https://cloud.google.com/functions/docs/writing/background) for details.\n\nThe context parameter is a Go `context.Context`, and contains additional event\nmetadata under a functions-specific key. This data is accesible via the `cloud.google.com/go/functions/metadata` package:\n\n```golang\nm := metadata.FromContext(ctx)\n```\n\nThese functions can be registered in `main.go` for local testing with the handler via `funcframework.RegisterEventFunctionContext`.\n\n[ff_go_unit_img]: https://github.com/GoogleCloudPlatform/functions-framework-go/workflows/Go%20Unit%20CI/badge.svg\n[ff_go_unit_link]: https://github.com/GoogleCloudPlatform/functions-framework-go/actions?query=workflow%3A\"Go+Unit+CI\"\n[ff_go_lint_img]: https://github.com/GoogleCloudPlatform/functions-framework-go/workflows/Go%20Lint%20CI/badge.svg\n[ff_go_lint_link]: https://github.com/GoogleCloudPlatform/functions-framework-go/actions?query=workflow%3A\"Go+Lint+CI\"\n[ff_go_conformance_img]: https://github.com/GoogleCloudPlatform/functions-framework-go/workflows/Go%20Conformance%20CI/badge.svg\n[ff_go_conformance_link]: https://github.com/GoogleCloudPlatform/functions-framework-go/actions?query=workflow%3A\"Go+Conformance+CI\"\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgooglecloudplatform%2Ffunctions-framework-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgooglecloudplatform%2Ffunctions-framework-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgooglecloudplatform%2Ffunctions-framework-go/lists"}