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: themainpackage. 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: returnshelloand the value of theeventparameter that is passed in. Ifnilis returned, no error occurs.
func main(): the entry point for running function code in Function Compute. A Go application must contain themainfunction. You can addfc.Start(HandleRequest)to run your application in Function Compute.ImportantThe methods used to start an HTTP handler and an event handlers are different. To start an event handler, invoke the
fc.Startfunction in themainfunction. To start an HTTP handler, invoke thefc.StartHttpfunction in themainfunction.
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 () errorfunc (InputType) errorfunc () (OutputType, error)func (InputType) (OutputType, error)func (context.Context) errorfunc (context.Context, InputType) errorfunc (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
errortype. If two values are returned, the second value must indicate theerrormessage.
Sample code for event handlers for Go:
event-struct.go: the sample code for a handler whose
eventobject is of the STRUCT type.event-string.go: the sample code for a handler whose
eventobject is of the STRING type.event-map.go: the sample code for a handler whose
eventobject is of themap[string]interface{}type.
For more information about the sample code for other handlers, visit examples.
Context
For more information about the context, see Context.