Skip to main content

Run a Worker - Go SDK

This page covers long-lived Workers that you host and run as persistent processes. For Workers that run on serverless compute like AWS Lambda, see Serverless Workers.

Create and run a Worker

Create a Worker by calling worker.New() and passing:

  1. A Temporal Client.
  2. The name of the Task Queue to poll.
  3. A worker.Options struct (can be empty for defaults).

Register your Workflow and Activity types, then call Run() to start polling.

package main

import (
"log"

"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
)

func main() {
c, err := client.Dial(client.Options{})
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

w := worker.New(c, "my-task-queue", worker.Options{})
w.RegisterWorkflow(MyWorkflow)
w.RegisterActivity(MyActivity)

err = w.Run(worker.InterruptCh())
if err != nil {
log.Fatalln("Unable to start Worker", err)
}
}

Run() accepts an interrupt channel so the Worker shuts down on SIGINT or SIGTERM. You can also call Start() and Stop() separately for more control over the lifecycle.

tip

If you have gow installed, the Worker automatically reloads when you update the file:

go install github.com/mitranim/gow@latest
gow run worker/main.go

Connect to Temporal Cloud

To run a Worker against Temporal Cloud, configure the client connection with your Namespace address and authentication credentials. See Connect to Temporal Cloud for setup instructions.

Register Workflows and Activities

All Workers listening to the same Task Queue must be registered to handle the same Workflow Types and Activity Types. If a Worker polls a Task for a type it does not know about, the Task fails. The Workflow Execution itself does not fail.

Use RegisterWorkflow() and RegisterActivity() to register types. To register an Activity struct with multiple methods, pass the struct. The Worker gets access to all exported methods.

w.RegisterWorkflow(WorkflowA)
w.RegisterWorkflow(WorkflowB)
w.RegisterActivity(&MyActivities{})

To customize the registered name or other options, use RegisterWorkflowWithOptions() or RegisterActivityWithOptions(). See workflow.RegisterOptions and activity.RegisterOptions.

Worker options

Pass a worker.Options struct to worker.New() to configure concurrency limits, pollers, timeouts, and other Worker behavior. An empty struct uses defaults that work for most cases.

For the full list of options and their defaults, see the Go SDK reference.