-
Notifications
You must be signed in to change notification settings - Fork 0
Registering Functions
Chris O'Hara edited this page Jul 2, 2024
·
6 revisions
After setting up a Dispatch endpoint, the next step is to define and register a function.
Dispatch functions have the form:
func (ctx context.Context, input I) (output O, err error)I and O are any type that Dispatch can serialize:
- primitive values (booleans, integers, floats, strings, bytes)
- JSON-like values (primitive values, or lists and maps made up of JSON-like values)
- values that implement
encoding.BinaryMarshalerorencoding.TextMarshaler - values that implement
json.Marshaler - values that implement
proto.Message
Functions are defined using the dispatch.Func helper.
Here's an example function that converts an integer to a string:
stringify := dispatch.Func("stringify", func (ctx context.Context, input int) (string, error) {
return strconv.Itoa(input), nil
})Functions can be registered with a Dispatch endpoint when the endpoint is created.
For example, to register the stringify function defined above:
endpoint, err := dispatch.New(stringify)Alternatively, functions can be registered after an endpoint has been created:
endpoint.Register(stringify)See how to call a function.