All Products
Search
Document Center

Function Compute:Event handlers

Last Updated:Aug 16, 2024

This topic describes the structure and characteristics of event handlers for Go.

Sample code for using an event handler

Import an official SDK library named github.com/aliyun/fc-runtime-go-sdk/fc for Go and implement the handler and main functions. Sample code:

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, as shown in the following sample code:

{
  "key": "value"
}

The following content describes the code snippets in the sample code:

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

  • import: imports Function Compute dependencies. You need to import the following dependencies:

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

    • context: the context object of Function Compute SDK for Go.

  • func HandleRequest(ctx context.Context, event StructEvent) (string, error): the handler used to process event requests. The handler contains the code to be run and involves the following parameters:

    • ctx context.Context: provides the runtime context information when a function is invoked in Function Compute. For more information, see Context.

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

    • string, error: returns the STRING error type and the error message. For more information, see Error handling.

    • return fmt.Sprintf("hello,%s ! ", event.Key), nil: returns hello and the value of the event parameter that is passed in. If nil is returned, no error occurs.

  • func main(): the entry point for running function code in Function Compute. A Go application must contain the main function. You can add fc.Start(HandleRequest) to run your application in Function Compute.

    Important

    The methods used to start an HTTP handler and an event handlers are different. To start an event handler, invoke the fc.Start function in the main function. To start an HTTP handler, invoke the fc.StartHttp function in the main function.

Signatures for event handlers

The following items list the valid signatures for event handlers. InputType and OutputType objects are compatible with the encoding/json standard library.

Function Compute deserializes the InputType objects by using the json.Unmarshal method and serializes OutputType objects by using the json.Marshal method. For more information about how to deserialize the data 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 an event 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.

Sample code for event handlers for Go:

  • 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, visit examples.

Context

For more information about the context, see Context.