@@ -20,8 +20,6 @@ Workflows are the fundamental unit of a Temporal Application, and it all starts
2020In the Temporal Go SDK programming model, a [ Workflow Definition] ( /workflow-definition ) is an exportable function.
2121Below is an example of a basic Workflow Definition.
2222
23- <ViewSourceCodeNotice href = " https://github.com/temporalio/documentation/blob/main/sample-apps/go/yourapp/your_workflow_definition_dacx.go" />
24-
2523``` go
2624package yourapp
2725
@@ -30,9 +28,6 @@ import (
3028
3129 " go.temporal.io/sdk/workflow"
3230)
33- // ...
34-
35- // YourSimpleWorkflowDefinition is the most basic Workflow Definition.
3631func YourSimpleWorkflowDefinition (ctx workflow .Context ) error {
3732 // ...
3833 return nil
@@ -59,9 +54,7 @@ However, the best practice is to pass a single parameter that is of a `struct` t
5954
6055All Workflow Definition parameters must be serializable and can't be channels, functions, variadic, or unsafe pointers.
6156
62- <ViewSourceCodeNotice href = " https://github.com/temporalio/documentation/blob/main/sample-apps/go/yourapp/your_workflow_definition_dacx.go" />
63-
64- ``` go
57+ ``` go {9-12,14}
6558package yourapp
6659
6760import (
@@ -70,15 +63,30 @@ import (
7063 " go.temporal.io/sdk/workflow"
7164)
7265
73- // YourWorkflowParam is the object passed to the Workflow.
7466type YourWorkflowParam struct {
75- WorkflowParamX string
76- WorkflowParamY int
67+ WorkflowParamX string
68+ WorkflowParamY int
7769}
78- // ...
79- // YourWorkflowDefinition is your custom Workflow Definition.
70+
8071func YourWorkflowDefinition (ctx workflow .Context , param YourWorkflowParam ) (*YourWorkflowResultObject , error ) {
81- // ...
72+ activityOptions := workflow.ActivityOptions {
73+ StartToCloseTimeout: 10 * time.Second ,
74+ }
75+ ctx = workflow.WithActivityOptions (ctx, activityOptions)
76+ activityParam := YourActivityParam{
77+ ActivityParamX: param.WorkflowParamX ,
78+ ActivityParamY: param.WorkflowParamY ,
79+ }
80+
81+ var a *YourActivityObject
82+
83+ var activityResult YourActivityResultObject
84+
85+ err := workflow.ExecuteActivity (ctx, a.YourActivityDefinition , activityParam).Get (ctx, &activityResult)
86+ if err != nil {
87+ return nil , err
88+ }
89+ return nil
8290}
8391```
8492
@@ -95,31 +103,41 @@ However, it's not possible to receive both a custom value and an error in the ca
95103The caller will receive either one or the other.
96104Returning a non-nil ` error ` from a Workflow indicates that an error was encountered during its execution and the Workflow Execution should be terminated, and any custom return values will be ignored by the system.
97105
98- <ViewSourceCodeNotice href = " https://github.com/temporalio/documentation/blob/main/sample-apps/go/yourapp/your_workflow_definition_dacx.go" />
99-
100- ``` go
106+ ``` go {11-14,35-38}
101107package yourapp
102108
103109import (
104110 " time"
105111
106112 " go.temporal.io/sdk/workflow"
107113)
108- // ...
109114
110- // YourWorkflowResultObject is the object returned by the Workflow.
115+ // other structs and code
116+
111117type YourWorkflowResultObject struct {
112118 WFResultFieldX string
113119 WFResultFieldY int
114120}
115- // ...
116- // YourWorkflowDefinition is your custom Workflow Definition.
121+
117122func YourWorkflowDefinition (ctx workflow .Context , param YourWorkflowParam ) (*YourWorkflowResultObject , error ) {
118- // ...
123+ activityOptions := workflow.ActivityOptions {
124+ StartToCloseTimeout: 10 * time.Second ,
125+ }
126+ ctx = workflow.WithActivityOptions (ctx, activityOptions)
127+ activityParam := YourActivityParam{
128+ ActivityParamX: param.WorkflowParamX ,
129+ ActivityParamY: param.WorkflowParamY ,
130+ }
131+
132+ var a *YourActivityObject
133+
134+ var activityResult YourActivityResultObject
135+
136+ err := workflow.ExecuteActivity (ctx, a.YourActivityDefinition , activityParam).Get (ctx, &activityResult)
119137 if err != nil {
120138 return nil , err
121139 }
122- // Make the results of the Workflow Execution available.
140+
123141 workflowResult := &YourWorkflowResultObject{
124142 WFResultFieldX: activityResult.ResultFieldX ,
125143 WFResultFieldY: activityResult.ResultFieldY ,
@@ -134,9 +152,7 @@ In Go, by default, the Workflow Type name is the same as the function name.
134152
135153To customize the Workflow Type, set the ` Name ` parameter with ` RegisterOptions ` when registering your Workflow with a Worker.
136154
137- <ViewSourceCodeNotice href = " https://github.com/temporalio/documentation/blob/main/sample-apps/go/yourapp/worker/main_dacx.go" />
138-
139- ``` go
155+ ``` go {20,24-27}
140156package main
141157
142158import (
@@ -149,17 +165,38 @@ import (
149165
150166 " documentation-samples-go/yourapp"
151167)
152- // ...
153168func main () {
154- // ...
169+ temporalClient , err := client.Dial (client.Options {})
170+ if err != nil {
171+ log.Fatalln (" Unable to create client" , err)
172+ }
173+ defer temporalClient.Close ()
174+
155175 yourWorker := worker.New (temporalClient, " your-custom-task-queue-name" , worker.Options {})
156- // ...
157- // Use RegisterOptions to set the name of the Workflow Type for example.
176+
177+ yourWorker.RegisterWorkflow (yourapp.YourWorkflowDefinition )
178+
158179 registerWFOptions := workflow.RegisterOptions {
159180 Name: " JustAnotherWorkflow" ,
160181 }
161182 yourWorker.RegisterWorkflowWithOptions (yourapp.YourSimpleWorkflowDefinition , registerWFOptions)
162- // ...
183+
184+ message := " This could be a connection string or endpoint details"
185+ number := 100
186+ activities := &yourapp.YourActivityObject {
187+ Message: &message,
188+ Number: &number,
189+ }
190+
191+ registerAOptions := activity.RegisterOptions {
192+ Name: " JustAnotherActivity" ,
193+ }
194+ yourWorker.RegisterActivityWithOptions (yourapp.YourSimpleActivityDefinition , registerAOptions)
195+
196+ err = yourWorker.Run (worker.InterruptCh ())
197+ if err != nil {
198+ log.Fatalln (" Unable to start Worker" , err)
199+ }
163200}
164201```
165202
0 commit comments