Hi,
I’ve been looking at go-samples and I’ve noticed that most of the loop defines futures, the selectors, and timers inside the main for {}
is there a specific reason it’s not defined outside of the for {} and just selector.Select(ctx) is called?
For example:
In the await-signals sample:
func (a *AwaitSignals) Listen(ctx workflow.Context) {
log := workflow.GetLogger(ctx)
for {
selector := workflow.NewSelector(ctx)
selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal1"), func(c workflow.ReceiveChannel, more bool) {
c.Receive(ctx, nil)
a.Signal1Received = true
log.Info("Signal1 Received")
})
selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal2"), func(c workflow.ReceiveChannel, more bool) {
c.Receive(ctx, nil)
a.Signal2Received = true
log.Info("Signal2 Received")
})
selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal3"), func(c workflow.ReceiveChannel, more bool) {
c.Receive(ctx, nil)
a.Signal3Received = true
log.Info("Signal3 Received")
})
selector.Select(ctx)
if a.FirstSignalTime.IsZero() {
a.FirstSignalTime = workflow.Now(ctx)
}
}
}
Could this not be written as:
func (a *AwaitSignals) Listen(ctx workflow.Context) {
log := workflow.GetLogger(ctx)
selector := workflow.NewSelector(ctx)
selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal1"), func(c workflow.ReceiveChannel, more bool) {
c.Receive(ctx, nil)
a.Signal1Received = true
log.Info("Signal1 Received")
})
selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal2"), func(c workflow.ReceiveChannel, more bool) {
c.Receive(ctx, nil)
a.Signal2Received = true
log.Info("Signal2 Received")
})
selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal3"), func(c workflow.ReceiveChannel, more bool) {
c.Receive(ctx, nil)
a.Signal3Received = true
log.Info("Signal3 Received")
})
for {
selector.Select(ctx)
if a.FirstSignalTime.IsZero() {
a.FirstSignalTime = workflow.Now(ctx)
}
}
}
So my question is what absolutely has to go into the for {} or is there not even a difference?