You need to enable JavaScript to run this app.
Volcano Engine Function as a Service

Volcano Engine Function as a Service

Copy page
Download PDF
Golang runtime
Development methods
Copy page
Download PDF
Development methods
For Golang functions, veFaaS provides a Runtime SDK for you to develop your functions. You need to import the Runtime SDK into your local code. veFaaS uses the entry function exposed by the Runtime SDK to start the veFaaS process and handle HTTP requests or Event requests.​
Startup function​
Currently, veFaaS provides two function startup entry points: Start(handler interface{}) and StartWithInitializer(handler interface{}, initializer interface{}). Select the appropriate startup function based on your actual needs.​
Start function​
In general scenarios, you can choose the Start function as the startup function.​
You need to provide a handler function as an input parameter to this function. The handler function is used to process the specific business logic. For a detailed introduction to the handler function, see handler function section below in this topic.​
The following is a Golang code example that uses the Start function to start the service.​
​
package main​
​
import (​
"context"​
"encoding/json"​
"fmt"​
​
"github.com/volcengine/vefaas-golang-runtime/events"​
"github.com/volcengine/vefaas-golang-runtime/vefaas"​
"github.com/volcengine/vefaas-golang-runtime/vefaascontext"​
)​
​
func main() {​
// Start your vefaas function.​
vefaas.Start(handler)​
}​
​
// Define your handler function.​
func handler(ctx context.Context, r *events.HTTPRequest) (*events.EventResponse, error) {​
fmt.Printf("request id: %v", vefaascontext.RequestIdFromContext(ctx))​
fmt.Printf("request headers: %v", r.Headers)​
​
body, _ := json.Marshal(map[string]string{"message": "Hello veFaaS!"})​
return &events.EventResponse{​
Headers: map[string]string{​
"Content-Type": "application/json",​
},​
Body: body,​
}, nil​
}​
​
StartWithInitializer function​
If your function has some global initialization logic, such as loading configuration files, establishing a connection to a database, or initializing downstream clients, we recommend using the StartWithInitializer function as the startup function.​
You need to provide two parameters to this function.​
  • handler function: It is used to process the specific business logic. For a detailed introduction to the handler function, see handler function section below in this topic.​
  • initializer function: It is used for initializing function instances. For details about the initializer function, see initializer function section below in this topic.​
The following is a Golang code example that uses the StartWithInitializer function to start the service.​
​
package main​
​
import (​
"context"​
"encoding/json"​
"fmt"​
​
"github.com/volcengine/vefaas-golang-runtime/events"​
"github.com/volcengine/vefaas-golang-runtime/vefaas"​
"github.com/volcengine/vefaas-golang-runtime/vefaascontext"​
)​
​
func main() {​
// Start your vefaas function.​
vefaas.StartWithInitializer(handler, initializer)​
}​
​
// Define your handler function.​
func handler(ctx context.Context, r *events.HTTPRequest) (*events.EventResponse, error) {​
fmt.Printf("request id: %v", vefaascontext.RequestIdFromContext(ctx))​
fmt.Printf("request headers: %v", r.Headers)​
​
body, _ := json.Marshal(map[string]string{"message": "Hello veFaaS!"})​
return &events.EventResponse{​
Headers: map[string]string{​
"Content-Type": "application/json",​
},​
Body: body,​
}, nil​
}​
​
func initializer(ctx context.Context) error {​
// Do the initialization stuff, e.g., setup database connection,​
// http client for the downstream, or others, which may involve the declaration​
// and initialization of some global variables.​
//​
// Return error (recommended) or just panic (veFaaS will help recover) here if​
// something wrong happened.​
​
return nil​
}​
​
handler function​
The handler is the entry point for the function's request processing logic. The handler contains the user's core business logic. It is used to receive HTTP or event-type requests, process them, and ultimately return a response.​
Based on the type of request received, the handler function signature can be categorized into the following three types. Use the appropriate signature based on your actual needs.​
Handling regular HTTP requests​
The function signature for this type is as follows. It receives an HTTP request and the corresponding Context, and returns an EventResponse along with any possible Error that occurs during request processing. This is suitable for scenarios where veFaaS functions are invoked via API Gateway triggers.​
​
// Handle regular HTTP request.​
func(context.Context, *events.HTTPRequest) (*events.EventResponse, error)​
​
Parameter description​
​
Parameter​
Description​
context.Context​
It represents runtime information, including request timeout and request ID. For details, see vefaas-golang-runtime/vefaascontext/context.go.​
The request ID is a UUID generated by veFaaS for each request. It can be extracted from the Context using the vefaascontext.RequestIdFromContext function in the code sample above.​
events.HTTPRequest​
It represents an HTTP request encapsulated by veFaaS. It contains the metadata required for processing the HTTP request, such as the request method, header information, and request payload. For the specific data structure definition, see vefaas-golang-runtime/events/http.go.​
events.EventResponse​
It is used for the function to return the result of request execution, such as status code, header information, and payload. For the detailed data structure definition, see vefaas-golang-runtime/events/response.go.​
error​
It provides a convenient way to handle errors.​
If the handler returns a non-nil error, the status code in the final response will be set to 500, and the error message will be encapsulated in the response header (you can extract it via the X-Faas-Response-Error-Message). In this case, the events.EventResponse returned by the user's handler will be ignored.​
warning​
events.EventResponse and error cannot both be nil.​
​
Handling CloudEvent requests​
The signature for this type is shown below. It receives a CloudEvent-format request encapsulated by veFaaS and the corresponding Context, and returns an EventResponse along with any possible Error that occurs during request processing. This is suitable for handling trigger scenarios of event types such as Timer triggers and Kafka triggers.​
​
// Handle CloudEvent request.​
func(context.Context, *events.CloudEvent) (*events.EventResponse, error)​
​
Parameter description​
  • context.Context, events.EventResponse, and error parameter descriptions are the same as those for handler functions processing regular HTTP requests.​
  • For descriptions of the remaining parameters, see the table below.​
​
Parameter​
Description​
events.CloudEvent​
It represents a CloudEvent-type event simply encapsulated by veFaaS. It contains the event's context information, whether the payload is base64-encoded, and the specific payload. For the detailed data structure definition, see vefaas-golang-runtime/events/cloudevent.go.​
​
Handling arbitrary types of requests​
The signature for this type is shown below. It is suitable for scenarios where a single function needs to receive both regular HTTP requests and event-type requests. Inside the handler function, you can further distinguish the request type using Golang's type assertion and handle it accordingly.​
​
// Handle request of any type.​
func(context.Context, interface{}) (*events.EventResponse, error)​
​
Parameter description​
  • context.Context, events.EventResponse, and error parameter descriptions are the same as those for handler functions processing regular HTTP requests.​
  • The following is an example of handling a payload of type interface{}.​
​
func handler(ctx context.Context, payload interface{}) (*events.EventResponse, error) {​
switch event := payload.(type) {​
case *events.HTTPRequest:​
fmt.Printf("received http request, %v", event)​
// Handle regular HTTP request.​
return &events.EventResponse{StatusCode: http.StatusOK}, nil​
case *events.CloudEvent:​
fmt.Printf("received CloudEvent request, %v", event)​
// Handle CloudEvent request.​
return &events.EventResponse{StatusCode: http.StatusOK}, nil​
default:​
errMsg := fmt.Sprintf("unexpected payload type %T", payload)​
fmt.Fprintln(os.Stderr, errMsg)​
body, _ := json.Marshal(errMsg)​
return &events.EventResponse{​
StatusCode: http.StatusBadRequest,​
Body: body,​
}, nil​
}​
}​
​
initializer function​
The initializer is the entry point for the function's initialization logic. For instance-level global initialization within a single function, we recommend using the initializer function.​
Function signature​
The initializer function signatures currently supported by veFaaS are shown below. You can indicate initialization failure by returning an error (recommended) or by triggering a panic.​
​
func(context.Context) error​
​
Parameter description​
context.Context and error descriptions are the same as those for handler functions processing regular HTTP requests.​
Use environment variables​
For veFaaS functions, you can configure the environment variables required by the runtime of your function in the console and retrieve them in your code to implement function processing logic. veFaaS injects configured environment variables into the container where the function runs, and the program can read them using a built-in library for the programming language.​
For example, if the key of an environment variable is envKey, you can use the following code sample to read the environment variable.​
​
import "os"​
​
envValue := os.Getenv("envKey")​
​
​
Last updated: 2026.08.19 14:06:05