Help Center/ FunctionGraph/ Developer Guide/ Go/ Function Development Overview
Updated on 2025-12-10 GMT+08:00

Function Development Overview

Function Syntax

FunctionGraph supports Go 1.x. The following shows the function syntax.

func Handler (payload []byte, ctx context.RuntimeContext)
  • Handler: name of the handler function.
  • payload: event parameter defined for the function. The parameter is in JSON format.
  • ctx: runtime information provided for executing the function. For details, see SDK APIs.

The format of the Go function handler is the same as the name of the executable file in the code package. Ensure that the name of the dynamic library file is consistent with the plug-in name of the handler. For example, if the name of the dynamic library file is testplugin.so, set the handler name to testplugin.Handler. You can configure or modify the handler on the function details page on the FunctionGraph console.

SDK APIs

The Go SDK provides event, context, and logging APIs. Download the Go SDK (Go SDK.sha256).

  • Event APIs

    Event structure definitions are added to the Go SDK. Currently, DIS, DDS, SMN, Timer, and APIG triggers are supported. The definitions make coding much simpler when triggers are required.

    1. APIG trigger field description
      1. APIGTriggerEvent fields
        Table 1 APIGTriggerEvent fields

        Field

        Description

        IsBase64Encoded

        Whether the body of an event is encoded using Base64.

        HttpMethod

        HTTP request method.

        Path

        HTTP request path.

        Body

        HTTP request body.

        PathParameters

        All path parameters.

        RequestContext

        API Gateway configurations (APIGRequestContext object).

        Headers

        HTTP request header.

        QueryStringParameters

        Query parameters.

        UserData

        User data set in the APIG custom authorizer.

        Table 2 APIGRequestContext fields

        Field

        Description

        ApiId

        API ID.

        RequestId

        API request ID.

        Stage

        Name of the environment in which an API has been published.

      2. APIGTriggerResponse fields
        Table 3 APIGTriggerResponse fields

        Field

        Description

        Body

        Message body.

        Headers

        HTTP response header to be returned.

        StatusCode

        HTTP status code. Type: int.

        IsBase64Encoded

        Whether the body has been encoded using Base64. Type: bool.

        APIGTriggerEvent provides the GetRawBody() method to obtain the body decoded using Base64. APIGTriggerResponse provides the SetBase64EncodedBody() method to set the body encoded using Base64.

    2. DIS trigger field description
      Table 4 DISTriggerEvent fields

      Field

      Description

      ShardID

      Partition ID.

      Message

      DIS message body (DISMessage structure).

      Tag

      Function version.

      StreamName

      Stream name.

      Table 5 DISMessage fields

      Field

      Description

      NextPartitionCursor

      Next partition cursor.

      Records

      Message records (DISRecord structure).

      MillisBehindLatest

      Reserved parameter.

      Table 6 DISRecord fields

      Field

      Description

      PartitionKey

      Data partition.

      Data

      Data.

      SequenceNumber

      Sequence number (ID of each record).

    3. Kafka trigger field description
      Table 7 KAFKATriggerEvent fields

      Field

      Description

      InstanceId

      Instance ID.

      Records

      Message records (Table 8).

      TriggerType

      Trigger type (Kafka).

      Region

      Region.

      EventTime

      Time when an event occurred (seconds).

      EventVersion

      Event version.

      Table 8 KAFKARecord parameters

      Field

      Description

      Messages

      DMS message body.

      TopicId

      Topic ID.

    4. SMN trigger field description
      Table 9 SMNTriggerEvent fields

      Field

      Description

      Record

      Message records (SMNRecord structure).

      Table 10 SMNRecord fields

      Field

      Description

      EventVersion

      Event version. (Currently, the version is 1.0.)

      EventSubscriptionUrn

      Subscription URN.

      EventSource

      Event source.

      Smn

      Message body (SMNBody structure).

      Table 11 SMNBody fields

      Field

      Description

      TopicUrn

      Topic URN.

      TimeStamp

      Message timestamp.

      MessageAtrributes

      Message attribute set.

      Message

      Message body.

      Type

      Message format.

      MessageId

      Message ID.

      Subject

      Message topic.

    5. Timer trigger field description
      Table 12 TimerTriggerEvent fields

      Field

      Description

      Version

      Version. (Currently, the version is v1.0.)

      Time

      Current time.

      TriggerType

      Trigger type (Timer).

      TriggerName

      Trigger name.

      UserEvent

      Additional information about the trigger.

    1. When using an APIG trigger, set the first parameter of the handler function (for example, handler) to handler(APIGTriggerEvent event, Context context). For details about the constraints, see Base64 Decoding and Response Structure.
    2. The preceding TriggerEvent methods have corresponding set methods, which are recommended for local debugging. DIS and LTS triggers have getRawData() methods, but do not have setRawData() methods.
  • Context APIs

    The context APIs are used to obtain the context, such as agency AK/SK, current request ID, allocated memory space, and number of CPUs, required for executing a function.

    Table 13 describes the context APIs provided by FunctionGraph.

    Table 13 Context methods

    Method

    Description

    getRequestID( )

    Obtains a request ID.

    getRemainingTimeInMilligetRunningTimeInSecondsSeconds ( )

    Obtains the remaining running time of a function.

    getAccessKey( )

    Obtains the AK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    FunctionGraph has stopped maintaining the getAccessKey API in the Runtime SDK. You cannot use this API to obtain a temporary AK.

    getSecretKey( )

    Obtains the SK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    FunctionGraph has stopped maintaining the getSecretKey API in the Runtime SDK. You cannot use this API to obtain a temporary SK.

    getSecurityAccessKey( )

    Obtains the SecurityAccessKey (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function.

    getSecuritySecretKey( )

    Obtains the SecuritySecretKey (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function.

    getSecurityToken( )

    Obtains the SecurityToken (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function.

    getUserData(string key)

    Uses keys to obtain the values passed by environment variables.

    getFunctionName( )

    Obtains the name of a function.

    getRunningTimeInSeconds ( )

    Obtains the timeout of a function.

    getVersion( )

    Obtains the version of a function.

    getMemorySize( )

    Obtains the allocated memory.

    getCPUNumber( )

    Obtains CPU usage of a function.

    getPackage( )

    Obtains a function group.

    getToken( )

    Obtains the token (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    getLogger( )

    Obtains the logger method provided by the context. By default, information such as the time and request ID is output.

    getAlias()

    Obtains function alias.

  • Table 14 describes the logging API provided in the Go SDK.
    Table 14 Logging API

    Method

    Description

    RuntimeLogger()

    • Records user input logs by using the method Logf(format string, args ...interface{}).
    • This method outputs logs in the format of Time-Request ID-Output, for example, 2017-10-25T09:10:03.328Z 473d369d-101a-445e-a7a8-315cca788f86 test log output.