Dependencies Example
This example demonstrates how to create steps with complex dependencies and ensure they execute in the correct order, using a data processing scenario.
Overview
The example shows:
- How to create multiple steps with different operations
- How to set up complex dependencies between steps
- How the steps are automatically sorted and executed in dependency order
- How to maintain proper execution flow through dependencies
Key Concepts
- Step Dependencies: Using
DependencyAdd to specify that one step depends on another
- Topological Sorting: The steps are automatically sorted to ensure dependencies are respected
- Execution Order: Steps are executed in the order determined by their dependencies
- Complex Dependencies: Steps can depend on multiple other steps
Data Processing Flow
The example demonstrates a simple data processing pipeline:
Set Initial Data: Initializes an array of numbers [1, 2, 3]
Process Data: Multiplies each number by 2 (result: [2, 4, 6])
Calculate Sum: Calculates the sum of the processed numbers (result: 12)
Running the Example
To run this example:
# Run the main program
go run main.go
# Run the tests
go test -v
Expected Output
The program will output:
Final sum: 12
Steps completed: [Set Initial Data Process Data Calculate Sum]
Data Processing Process
The steps perform the following operations in order:
Set Initial Data: Creates an array with numbers [1, 2, 3]
Process Data: Multiplies each number by 2 (result: [2, 4, 6])
Calculate Sum: Adds up all numbers (result: 12)
The final sum is calculated as:
- Start with initial numbers: [1, 2, 3]
- Double each number: [2, 4, 6]
- Calculate sum: 2 + 4 + 6 = 12
Implementation Details
Step Dependencies
Process Data depends on Set Initial Data
Calculate Sum depends on Process Data
DAG Structure
The DAG is created with three steps:
Set Initial Data - Creates the initial data
Process Data - Processes the data
Calculate Sum - Calculates the final result
The dependencies ensure that each step only runs after its dependencies have completed.