How to handle Workflow logic requirements in Go
In Go, Workflow Definition code cannot directly do the following:
- Iterate over maps using
range, because withrangethe order of the map's iteration is randomized. Instead you can collect the keys of the map, sort them, and then iterate over the sorted keys to access the map. This technique provides deterministic results. You can also use a Side Effect or an Activity to process the map instead. - Call an external API, conduct a file I/O operation, talk to another service, etc. (Use an Activity for these.)
The Temporal Go SDK has APIs to handle equivalent Go constructs:
workflow.Now()This is a replacement fortime.Now().workflow.Sleep()This is a replacement fortime.Sleep().workflow.GetLogger()This ensures that the provided logger does not duplicate logs during a replay.workflow.Go()This is a replacement for thegostatement.workflow.ChannelThis is a replacement for the nativechantype. Temporal provides support for both buffered and unbuffered channels.workflow.SelectorThis is a replacement for theselectstatement. Learn more on the Go SDK Selectors pageworkflow.ContextThis is a replacement forcontext.Context. Learn more on the Go SDK Context Propagation page.