Skip to content

docs(sdk): add basic workflow example #691

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 4 commits into from
Feb 21, 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
85 changes: 85 additions & 0 deletions daprdocs/content/en/go-sdk-docs/go-client/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,91 @@ resp, err = client.InvokeMethodWithContent(ctx, "app-id", "method-name", "post",

For a full guide on service invocation, visit [How-To: Invoke a service]({{< ref howto-invoke-discover-services.md >}}).

### Workflows

Workflows and their activities can be authored and managed using the Dapr Go SDK like so:

```go
import (
...
"github.com/dapr/go-sdk/workflow"
...
)

func ExampleWorkflow(ctx *workflow.WorkflowContext) (any, error) {
var output string
input := "world"

if err := ctx.CallActivity(ExampleActivity, workflow.ActivityInput(input)).Await(&output); err != nil {
return nil, err
}

// Print output - "hello world"
fmt.Println(output)

return nil, nil
}

func ExampleActivity(ctx workflow.ActivityContext) (any, error) {
var input int
if err := ctx.GetInput(&input); err != nil {
return "", err
}

return fmt.Sprintf("hello %s", input), nil
}

func main() {
// Create a workflow worker
w, err := workflow.NewWorker()
if err != nil {
log.Fatalf("error creating worker: %v", err)
}

// Register the workflow
w.RegisterWorkflow(ExampleWorkflow)

// Register the activity
w.RegisterActivity(ExampleActivity)

// Start workflow runner
if err := w.Start(); err != nil {
log.Fatal(err)
}

// Create a workflow client
wfClient, err := workflow.NewClient()
if err != nil {
log.Fatal(err)
}

// Start a new workflow
id, err := wfClient.ScheduleNewWorkflow(context.Background(), "ExampleWorkflow")
if err != nil {
log.Fatal(err)
}

// Wait for the workflow to complete
metadata, err := wfClient.WaitForWorkflowCompletion(ctx, id)
if err != nil {
log.Fatal(err)
}

// Print workflow status post-completion
fmt.Println(metadata.RuntimeStatus)

// Shutdown Worker
w.Shutdown()
}
```

- For a more comprehensive guide on workflows visit these How-To guides:
- [How-To: Author a workflow]({{< ref howto-author-workflow.md >}}).
- [How-To: Manage a workflow]({{< ref howto-manage-workflow.md >}}).
- Visit the Go SDK Examples to jump into complete examples:
- [Workflow Example](https://github.com/dapr/go-sdk/tree/main/examples/workflow)
- [Workflow - Parallelised](https://github.com/dapr/go-sdk/tree/main/examples/workflow-parallel)

### State Management

For simple use-cases, Dapr client provides easy to use `Save`, `Get`, `Delete` methods:
Expand Down
Loading