Release Notes
Announcements
package mainimport ("context""fmt""github.com/tencentyun/scf-go-lib/cloudfunction")type DefineEvent struct {// test event defineKey1 string `json:"key1"`Key2 string `json:"key2"`}func hello(ctx context.Context, event DefineEvent) (string, error) {fmt.Println("key1:", event.Key1)fmt.Println("key2:", event.Key2)return fmt.Sprintf("Hello %s!", event.Key1), nil}func main() {// Make the handler available for Remote Procedure Call by Cloud Functioncloudfunction.Start(hello)}
package main to include the main function.github.com/tencentyun/scf-go-lib/cloudfunction library by running go get github.com/tencentyun/scf-go-lib/cloudfunction before packaging and compilation.context needs to be in front, followed by event, and the combination of input parameters can be (), (event), (context), or (context, event). For more information, see Input parameters.ret needs to be in front, followed by error, and the combination of returned values can be (), (ret), (error), or (ret, error). For more information, see Returned values.event input parameter and ret returned value need to be compatible with the encoding/json standard library for Marshal and Unmarshal operations.main function by using the Start function inside the package.main, where main indicates that the executable entry file is the compiled main binary file.package and main functionsmain function is in the main package. In the main function, start the entry function that actually handles the business by using the Start function in the cloudfunction package.Start function in the package in the main function through import "github.com/tencentyun/scf-go-lib/cloudfunction".cloudfunction.Start, which usually handles the actual business. The input parameters and returned values of the entry function need to be written according to certain specifications.func hello()func hello(ctx context.Context)func hello(event DefineEvent)func hello(ctx context.Context, event DefineEvent)
context parameter is before the custom parameter.string or int) or custom data structures (such as DefineEvent in the sample). If a custom data structure is used, you need to make sure that the data structure is compatible with the encoding/json standard library for Marshal and Unmarshal operations; otherwise, an error will occur when the input parameters are passed in.func hello()()func hello()(error)func hello()(string, error)
string or int) or custom data structures. If a custom data structure is used, you need to make sure that it is compatible with the encoding/json standard library for Marshal and Unmarshal operations; otherwise, an error will occur due to exceptional conversion when the returned values are returned to the external API.피드백