Passing DB/Connection Objects to Activities


I am developing an API server in Go that will be using Temporal. My setup so far is as follows: each API endpoint has a corresponding handler function that does some boilerplate and then calls a workflow specific to the endpoint, which calls one or more activities specific to the endpoint.

So flow of execution when an endpoint is hit is: handler func → workflow → activity (each in its own package).

The activities within the workflows will require communication with DBs, external APIs, etc. I know it is not possible to pass these types of objects as inputs to workflows/activities as inputs must be serializable.

Currently, I’m simply passing DB/API configuration (instead of objects) via inputs to the activities, and spinning up/down the DB/API objects in each activity. I know this is probably not the best way to do this, and that the usual practice here is to define these objects on activity structs instead of using inputs.

This brings me to where I’m a little stuck.

I am currently creating my workers during the initial setup of the API service when the application spins up (away from any endpoint-specific logic), and registering workflows/activities to the workers there. To register an activity struct to a worker however, I must initialize an activity struct instance there, and register the instance.

My issue is now that I don’t know how the code where my activity execution is called (in its own package away from worker setup/registration) is supposed to have access to the activity struct instance I just created in my worker setup.

A couple of spitball solutions I’ve thought of:

  • Create the activity struct instance as a global variable (bad for obvious reasons)
  • Move the worker registration to where the activity execution call occurs in the workflow (not possible as workers cannot be passed as inputs, and wouldn’t really make sense either way)

I feel like there’s probably an obvious solution I’m not thinking of but any help would be appreciated.


The code that calls an activity doesn’t need to have access to the activity instance. Look at the Greetings sample, the nil pointer can be used to make the call.