All Products
Search
Document Center

Function Compute:Handlers

Last Updated:Feb 02, 2024

This topic describes the concepts, features, and examples of handlers in Go runtimes.

Note

If you want to use HTTP triggers or custom domain names to access functions, obtain request struct before you define HTTP responses. For more information, see Use an HTTP trigger to invoke a function.

What is a handler?

A handler for a function in Function Compute is used to process requests in function code. When a function in Function Compute is invoked, Function Compute uses the handler that you configured to process requests. To configure a handler, you can configure the Function Handler parameter in the Function Compute console.

The handler for Function Compute functions in Go is compiled into an executable binary file. You need to only set the handler of the Function Compute function to the name of the executable file.

For more information about the definitions and operations of functions in Function Compute, see Manage functions.

Configurations of handlers must conform to the configuration specifications of Function Compute. The configuration specifications vary based on the handler type.

Sample code

In code for Go, you must introduce the official SDK library aliyun/serverless/fc-runtime-go-sdk/fc and implement handler and main functions. The following sample code shows an example.

package main

import (
    "fmt"
    "context"

    "github.com/aliyun/fc-runtime-go-sdk/fc"
)

type StructEvent struct {
    Key string `json:"key"`
}

func HandleRequest(ctx context.Context, event StructEvent) (string, error) {
    return fmt.Sprintf("hello, %s!", event.Key), nil
}

func main() {
    fc.Start(HandleRequest)
}

The value of the event input parameter is a JSON string that contains the key information. The following sample code provides an example:

{
  "key": "value"
}

In the preceding code:

  • package main: the main package. Each Go application contains a main package.

  • import: imports Function Compute dependencies. You must import the following dependencies:

    • github.com/aliyun/fc-runtime-go-sdk/fc: the core library for Go in Function Compute.

    • context: the Go context object of Function Compute.

  • func HandleRequest(ctx context.Context, event StructEvent) (string, error): the handler, which must contain the code to execute. The following section describes the parameters:

    • ctx context.Context: the context information for your Function Compute function. For more information, see Context.

    • event StructEvent: the data to be passed in when the function is invoked. Multiple data types are supported.

    • string, error: the returned message, which contains a string and an error message. For more information, see Error handling.

    • return fmt.Sprintf("Hi, %s !", event.Key), nil: returns the hello information, which contains the input event. If nil is returned, no error occurs.

  • func main(): the entry point for running the code of the Function Compute function. Go programs must contain the main functions. The fc.Start(HandleRequest) code allows your program to run in Function Compute.

Event handler signatures

The following items list valid event handler signatures. Among these signatures, InputType and OutputType are compatible with the encoding/json standard library.

Function Compute deserializes the InputType input by using the json.Unmarshal method and serializes the returned OutputType by using the json.Marshal method. For information about how to deserialize the data that is returned by a function, see JSON Unmarshal.

  • func ()

  • func () error

  • func (InputType) error

  • func () (OutputType, error)

  • func (InputType) (OutputType, error)

  • func (context.Context) error

  • func (context.Context, InputType) error

  • func (context.Context) (OutputType, error)

  • func (context.Context, InputType) (OutputType, error)

You must use a handler based on the following rules:

  • The handler must be a function.

  • The handler can contain up to two input parameters. If the handler contains two input parameters, the first input parameter must be context.Context.

  • The handler can return up to two values. If only one value is returned, the value must indicate the error type. If two values are returned, the second value must indicate the error message.

The following section describes the sample code for handlers:

  • event-struct.go: the sample code for a handler whose event object is of the STRUCT type.

  • event-string.go: the sample code for a handler whose event object is of the STRING type.

  • event-map.go: the sample code for a handler whose event object is of the map[string]interface{} type.

For more information about the sample code for other handlers, see examples.

Context

For more information about how to use context, see Context.