Try Functions¶
Write a Go HTTP function, change its response, and run it locally with func.
No Kubernetes cluster, registry account, Dockerfile, or local Go toolchain is
required for the local workflow.
Prepare¶
- A running Docker engine and a Bash-compatible terminal (on Windows, use WSL 2 with Docker integration).
- Install the standalone
funcCLI:
brew install knative-extensions/kn-plugins/func
Download the binary for your OS and architecture from the
Functions release page (use a
Linux binary in WSL 2), make it executable, and place it on your PATH as
func.
Verify func version prints a version number. For kn func plugin installation,
see Develop locally — Prepare.
Create and change¶
-
Create a Go HTTP function in a fresh directory:
func create -l go -t http hello-function cd hello-function -
Open
function.go, findfmt.Fprintln(res, "OK")insideHandle, and replace it with:fmt.Fprintln(res, "Hello from my function!") -
Save the file and leave the rest of the template unchanged (
func.yamldescribes the project;function_test.gois a starter test).
Run and test¶
-
Inside
hello-function, build and run the function locally with buildpacks:func run --builder pack \ --image localhost/hello-function:latestThe first build downloads builder images and dependencies and can take several minutes.
localhost/hello-function:latestis a local image name — nothing is pushed or deployed. Wait for a line like:Function running on 127.0.0.1:8080 -
Keep that terminal open. In a second terminal inside the same
hello-functiondirectory, invoke the local function:func invoke --target localThe response includes
Hello from my function!. -
To iterate, press Ctrl+C in the first terminal, edit the message in
function.go, and re-runfunc runandfunc invoke --target local.
If the build exits or invoking fails
Read the final build error; the container builder needs a running Docker
engine (Docker documentation) and network access.
If invoking cannot find a local function, verify the first terminal is still
running and the second terminal is inside hello-function (ls func.yaml).
If stuck, get help with the error and func version.
Deploy to Knative (optional)¶
You have finished the local workflow. To run the same function on Knative, you need:
- A learning cluster — set up a cluster if you do not have one, and confirm your context and
defaultnamespace. -
Access to a container registry (logged in locally) with push permissions and a public image repository so the cluster can pull the image without pull secrets. Never include secrets in the project.
-
From
hello-function, build, push, and deploy (replace<registry>with your registry and namespace, for exampleghcr.io/aliceordocker.io/alice):func deploy --builder pack \ --image <registry>/hello-function:latest -
Invoke the remote Service explicitly:
func invoke --target remoteThe response contains the same message you tested locally. If the Service is not ready, verify the image is publicly pullable and inspect
kn service describe hello-function.
Next steps¶
Stop func run with Ctrl+C when finished. Continue with
Try Serving, Try Eventing,
or Next steps and cleanup.