In this episode we will improve our child programming skills by implementing two common patterns and
concurrent programming funding and funnel processes.
To do so we will introduce to channel specific context from the go programming language.
The first one is the select statement which is used to read from more than one channel.
At the same time the second is Channel direction which is used to increase our tape safety when we program
using channels.
So how does this process works.
Funding process is a process were given more than one channel.
In this case Channel A and Channel B will report their content and merge them into our third channel.
In this case child see the messages producers to channel a until we are going to be done by a function
call producer and the message is read from Channel see are going to be consumed by a process called
consumer.
The funding function will orchestrate all of this meaning that you will read from both tone to lean
on me and send the messages to channel C on the other hand find out that we have a single input channel
in this case channel a and we have a funnel process which given us a certain condition on those messages.
He will find all the messages to either channel me or Chelsea the producer and the consumer.
Semantics remains the same.
So as you can see they do the opposite.
Basically funding reach for many channels and right into a single one and find out reach from a single
channel and write to many others so let's get started by implementing our funding process to start with.
We will need to create a producer which is the function that will send information to a channel so let's
create a function called producer which is going to be receiving as an argument a channel let's call
it C H and it's going to be a channel of pipe into
now.
This is the first concept that I want to introduce which is Channel directions.
We can have a channel defined like this in this channel.
We could send messages and receive messages but if we do it like these we're saying that work is going
to be able to write right this time.
We'll never be able to be on the other side.
If we do like this using the Atlantic and decide rules we're saying that we can just read from the channel
and then try to it.
So in this case the producer will just write the channel soulless to write only town it's also useful
to have a name for the producers so we can differentiate from which producer is sending the messages
OK we have it like this.
So when the producer will do is basically forever loop it will slip some time then we will define the
same function.
So maybe some random time generate a random number on this so this will be an equals to generate randoms.
We will use the math run package.
The math the run package will create.
I would say no.
From zero to 100.
OK.
And then finally we will.
So the message is as simple as doing this right.
We could also bring something to the output.
So let's say Turnell is sending this.
The arguments will be the name of the town and the number but would just share it.
Now we wanted to create a sleep function.
This is going to be sleep is not receiving and returning anything and an hour.
Let's use the time package for these certain duration and the duration is going to be also a random
number.
So let's do random in
again let's say 3 seconds.
So what we're doing cuz we're saying we will sleep for a random amount of time between zero and three
thousand milliseconds which is going to be translating to from zero to three seconds.
OK.
So we have this and now we need to create the chance.
So let's say channel a b a channel of integers and in defining that sample we have three channels.
Channel 8 on B on set on Channel C as well.
So instead of h on the SC.
Now let's go and produce some messages on our aim is gonna be a let's do the same for channel.
So at this point we have two functions two producers sending messages to Channel 8 and 10 B.
And we're using the go keyword to not block in this execution because it's executing forever.
So this is going to continue and then we need to build some consumer and consumer will raise from Tennessee.
OK so let's write the consumer now.
So consumer will receive one channel.
It's going to be a child is but in this case the direction will be a read only child.
Right.
And the consumer function is a simple us forever range over the channel which is basically a reading
from the child and let's print the missiles.
So this will be format based on let's types these the value of the message is going to be like this.
And now wanting to do the orchestration which is different in function and the funding function will
receive a channel a B of input and that's how C S output is the core of our program.
So let's do it like this.
And it will sit a channel a and Channel B which are going to be read only channels.
So there's going to be eight and a channel see which is going to be a right only child.
So there's gonna be Child c of it.
OK.
Now basically the idea is that we will grade from Channel into B and write the content to channel C
so how do we do this on these were we introduce a new console which is select so let's define a variable
call and which is going to hold a value from any time.
And then again let's do a for loop and let's select from either channel a.
So we like this channel a or channel B OK.
Now what we're doing here is we're saying select from many of those channels and read from them.
If you find something do something.
And what we're gonna do is we're going to take the message and forward it to Chelsea.
So the thing is the producer is gonna sleep a certain amount of random time so we might have a case
where Channel a good messages twice and b still don't get any message but he's gonna be changing over
time.
We'll get a better picture when we run this.
So basically it is a defined function it reads from Channel 8 or channel B and sends a message to Chelsea.
If we haven't made any mistake let's execute this.
So this is going to be fun in the sleep.
Apparently it works.
Now let's analyze the output.
What happened is the channel a sends a message here forty seven and the message got grab from the consumer
channel they sent another message to 81 and got read from the consumer as well.
Now Jeremy sends a message and this is what we'll read and so on.
So to summarize channel a channel they send a message and then turn on B this channel a four.
One two three four five times and then turn to B if we execute these again it will probably change.
Going to be like random amount of messages going in and going out.
OK.
So this was fun in.
Now let's do fun out.
To do it we will copy the same code and we will modify a little bit.
So let's do it.
Obviously the funding function is not going to be needed anymore.
And the producer is not going to be named anymore because we have a single producer while we do have
is two different consumers.
So let's name the consumers.
US B and C.
Right.
Sorry.
This is not coming here.
We will do it here.
This is going to be the C consumer and this is going to be the the consumer.
So first of all the producer doesn't receive the streaming anymore.
So we will remove it from here.
The rest is going to be kind of the same.
So we will leave some time to not flood the channels with messages.
We will generate a random number and we will send it.
Now we don't have the name of the producer.
So let's just do this.
Let's just print the message we're sending and then I'll be like this.
Now the consumers we have two consumers 1 per out the town we need to name them in order to make sure
that you know which consumer is reading.
So let's do right.
And the consumers.
It's kind of doing the same but we're just bring up a more useful message.
So let's say the consumer is reading this let's do this.
And again our core function is the one is going to change now.
So let's write it song find out and find out is receiving one input channel Channel a it's going to
be a channel.
I read only channel and throughout the channels it's going to be channel B so C and that type of those
channels is going to be a channel.
Right.
So what is the final channel doing now.
So we've got to read from the Channel 8.
And we need to find some sort of condition let's say if the number we're generating numbers from zero
to 100.
Right.
So let's down the lower half from CEO to 15.
So if the number is below 50 we will send the message to channel the latest.
Otherwise else we will send the message in channel.
So the consumer will should just bring numbers below 50 and see consumer will should just be numbers
both right.
And now let's call the final function and we did this things correctly on those days.
Did you work.
Go find out.
OK.
So with Gerri the number of 87 it was consumed by hope we've made some mistakes.
Yes.
So something is happening here.
Let's try to develop these.
We are producing the messages.
And apparently she has a problem.
Yes.
We're reading from Channel A.
And we're writing the content channel V and Chelsea which seems to be fine.
OK.
This is the problem we're consuming from the same child.
So basically nobody was writing to channel B because nobody was reading from it.
So it wasn't getting fault.
Now it's better.
So let's do it.
What happened now.
Eighty seven is veteran fifty 59 as well the 18 which is below 50.
It's read by B.
So if you see every number below 50 it's handled by Channel B and the other one are handled by channel
C OK.
So this is basically it for funding and find out.
The idea was to kind of mastering the channels into these select statement and also the channel direction
which is super useful to increase the tape safety of them.
So I hope you have enjoyed.