Skip to content

Question on Selector in Go client #4421

Answered by longquanzheng
iamrodrigo asked this question in Q&A
Discussion options

You must be logged in to vote

Selector is to repalce native go select by workflow code for determinism.
Use workflow.NewSelector(ctx) method to create a Selector instance.
The interface is to simulate Golang's Select statement.
For example, the logic of Golang code like below

 	chA := make(chan int)
	chB := make(chan int)
	counter := 0
	for {
		select {
		case i, ok := <- chA:
			if ok{
				counter += i
			}
		case i, ok := <- chB:
			if ok{
				counter += i
			}
		}
	}

should be written as

 	s := workflow.NewSelector(ctx)
	counter := 0
	s.AddReceive(workflow.GetSignalChannel(ctx, "channelA"), func(c workflow.Channel, ok bool) {
		if ok{
			var i int
			c.Receive(ctx, &i)
			counter += i
		}
	})
	s.AddReceive(workflow.G…

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by longquanzheng
Comment options

You must be logged in to vote
1 reply
@Groxx
Comment options

Groxx Aug 26, 2021
Collaborator

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants