You are advised to use Go 1.x.
Syntax for creating a handler function in Go:
func Handler (payload []byte, ctx context.RuntimeContext)
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.
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. |
Results returned by using the GetToken(), GetAccessKey(), and GetSecretKey() methods contain sensitive information. Exercise caution when using these methods.
Table 2 describes the logging API provided in the Go SDK.
Method | Description |
|---|---|
RuntimeLogger() |
|
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. |