Skip to content

[Go] Dynamic Workflow/Activities #3698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions docs/develop/go/core-application.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ In this section you can find the following:
- [Start an Activity Execution](#activity-execution)
- [Run a dev Worker](#develop-worker)
- [Run a Temporal Cloud Worker](#run-a-temporal-cloud-worker)
- [Set a Dynamic Workflow](#set-a-dynamic-workflow)
- [Set a Dynamic Activity](#set-a-dynamic-activity)

## How to install the Temporal CLI and run a development server {#run-a-development-server}

Expand Down Expand Up @@ -1593,3 +1595,67 @@ registerOptions := activity.RegisterOptions{
w.RegisterActivityWithOptions(a.YourActivityDefinition, registerOptions)
// ...
```

## Set a Dynamic Workflow {#set-a-dynamic-workflow}

**How to set a Dynamic Workflow using the Temporal Go SDK**

A Dynamic Workflow in Temporal is a Workflow that is invoked dynamically at runtime if no other Workflow with the same name is registered.
A Workflow can be registered as dynamic by using `worker.RegisterDynamicWorkflow()`.
You must register the Workflow with the Worker before it can be invoked.
Only one Dynamic Workflow can be present on a Worker.

The Workflow Definition must then accept a single argument of type `converter.EncodedValues`.
This code snippet is taken from the [Dynamic Workflow example from samples-go](https://github.com/temporalio/samples-go/tree/main/dynamic-workflows).

```go
func DynamicWorkflow(ctx workflow.Context, args converter.EncodedValues) (string, error) {
var result string
info := workflow.GetInfo(ctx)

var arg1, arg2 string
err := args.Get(&arg1, &arg2)
if err != nil {
return "", fmt.Errorf("failed to decode arguments: %w", err)
}

if info.WorkflowType.Name == "dynamic-activity" {
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{StartToCloseTimeout: 10 * time.Second})
err := workflow.ExecuteActivity(ctx, "random-activity-name", arg1, arg2).Get(ctx, &result)
if err != nil {
return "", err
}
} else {
result = fmt.Sprintf("%s - %s - %s", info.WorkflowType.Name, arg1, arg2)
}

return result, nil
}
```

## Set a Dynamic Activity {#set-a-dynamic-activity}

**How to set a Dynamic Activity using the Temporal Go SDK**

A Dynamic Activity in Temporal is an Activity that is invoked dynamically at runtime if no other Activity with the same name is registered.
A Workflow can be registered as dynamic by using `worker.RegisterDynamicActivity()`.
You must register the Activity with the Worker before it can be invoked.
Only one Dynamic Activity can be present on a Worker.

The Workflow Definition must then accept a single argument of type `converter.EncodedValues`.
This code snippet is taken from the [Dynamic Workflow example from samples-go](https://github.com/temporalio/samples-go/tree/main/dynamic-workflows).

```go
func DynamicActivity(ctx context.Context, args converter.EncodedValues) (string, error) {
var arg1, arg2 string
err := args.Get(&arg1, &arg2)
if err != nil {
return "", fmt.Errorf("failed to decode arguments: %w", err)
}

info := activity.GetInfo(ctx)
result := fmt.Sprintf("%s - %s - %s", info.WorkflowType.Name, arg1, arg2)

return result, nil
}
```