Skip to content

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:

Defining a function

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
})

Registering a function

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)

Next steps

See how to call a function.

Clone this wiki locally