Function Syntax
You are advised to use Go 1.x.
Syntax for creating a handler function in Go:
func Handler (payload []byte, ctx context.RuntimeContext)
- Handler: name of the function that FunctionGraph invokes to execute your code. The name must be consistent with that you define when creating a function, and must start with an uppercase letter.
- 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.
SDK APIs
- Context APIs
The Go SDK provides multiple context APIs and a logging API. The download link of the Go SDK is provided in Table 1. Table 1 describes the context APIs provided by FunctionGraph.
Table 1 Context methods Method
Description
GetRequestID() string
Obtains a request ID.
GetRemainingTimeInMilliSeconds()
Obtains the remaining running time of a function.
GetAccessKey() string
Obtains the AK (valid for 24 hours) of an agency. If you use this method, you need to configure an agency for the function.
NOTE:FunctionGraph has stopped maintaining the getAccessKey API in the Runtime SDK. You cannot use this API to obtain a temporary AK.
GetSecretKey() string
Obtains the SK (valid for 24 hours) of an agency. If you use this method, you need to configure an agency for the function.
NOTE: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. If you use this method, you need to configure an agency for the function.
getSecuritySecretKey()
Obtains the SecuritySecretKey (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.
getSecurityToken()
Obtains the SecurityToken (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.
GetUserData(key string) string
Uses keys to obtain the values passed by environment variables.
GetFunctionName() string
Obtains the name of a function.
GetRunningTimeInSeconds() int
Obtains the timeout of a function.
GetVersion() string
Obtains the version of a function.
GetMemorySize() int
Obtains the allocated memory.
GetCPUNumber() int
Obtains CPU usage of a function.
GetProjectID() string
Obtains a project ID.
GetPackage() string
Obtains a function group, that is, an app.
GetToken() string
Obtains the token (valid for 24 hours) of an agency. If you use this method, you need to configure an agency for the function.
GetLogger() context.RuntimeLogger
Obtains the logger method provided by the context. By default, information such as the time and request ID is output.
NoticeResults returned by using the GetToken(), GetAccessKey(), and GetSecretKey() methods contain sensitive information. Exercise caution when using these methods.
- Logging API
Table 2 describes the logging API provided in the Go SDK.
Table 2 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.
Execution Result
The execution result consists of the function output, summary, and log output.
Parameter | Successful Execution | Failed Execution |
|---|---|---|
Function Output | The defined function output information is returned. | A JSON file that contains errorMessage and errorType is returned. The format is as follows:
errorMessage: Error message returned by the runtime. errorType: Error type. |
Summary | Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed. | Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed. |
Log Output | Function logs are printed. A maximum of 4 KB logs can be displayed. | Error information is printed. A maximum of 4 KB logs can be displayed. |