Quickstart: Run a Hello World function locally
mainTo run a Go function on your local machine, follow these steps:
Initialize the module:
go mod init example.com/helloCreate your function: Create a
function.gofile. Use thefunctions.HTTPmethod in aninit()function to register your handler.Create a local runner: Since the framework is designed for serverless environments, you need a
mainpackage to run it locally. Create acmd/main.gofile that usesfuncframework.StartHostPortto start the server. Use theFUNCTION_TARGETenvironment variable to specify which function to run.Run the server:
FUNCTION_TARGET=HelloWorld LOCAL_ONLY=true go run cmd/main.goTest the function:
curl localhost:8080
// function.go
package function
import (
"fmt"
"net/http"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
func init() {
functions.HTTP("HelloWorld", helloWorld)
}
func helloWorld(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, World!")
}