Skip to content

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 func CLI:
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

  1. Create a Go HTTP function in a fresh directory:

    func create -l go -t http hello-function
    cd hello-function
    
  2. Open function.go, find fmt.Fprintln(res, "OK") inside Handle, and replace it with:

    fmt.Fprintln(res, "Hello from my function!")
    
  3. Save the file and leave the rest of the template unchanged (func.yaml describes the project; function_test.go is a starter test).

Run and test

  1. Inside hello-function, build and run the function locally with buildpacks:

    func run --builder pack \
      --image localhost/hello-function:latest
    

    The first build downloads builder images and dependencies and can take several minutes. localhost/hello-function:latest is a local image name — nothing is pushed or deployed. Wait for a line like:

    Function running on 127.0.0.1:8080
    
  2. Keep that terminal open. In a second terminal inside the same hello-function directory, invoke the local function:

    func invoke --target local
    

    The response includes Hello from my function!.

  3. To iterate, press Ctrl+C in the first terminal, edit the message in function.go, and re-run func run and func 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 default namespace.
  • 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 example ghcr.io/alice or docker.io/alice):

    func deploy --builder pack \
      --image <registry>/hello-function:latest
    
  • Invoke the remote Service explicitly:

    func invoke --target remote
    

    The 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.

We use analytics and cookies to understand site traffic. Information about your use of our site is shared with Google for that purpose. Learn more.