WEBVTT 00:00.860 --> 00:06.800 In this episode we will improve our child programming skills by implementing two common patterns and 00:06.800 --> 00:10.200 concurrent programming funding and funnel processes. 00:10.250 --> 00:15.530 To do so we will introduce to channel specific context from the go programming language. 00:15.530 --> 00:20.830 The first one is the select statement which is used to read from more than one channel. 00:20.870 --> 00:27.170 At the same time the second is Channel direction which is used to increase our tape safety when we program 00:27.410 --> 00:28.270 using channels. 00:28.280 --> 00:30.980 So how does this process works. 00:30.980 --> 00:34.760 Funding process is a process were given more than one channel. 00:34.820 --> 00:41.870 In this case Channel A and Channel B will report their content and merge them into our third channel. 00:41.870 --> 00:50.960 In this case child see the messages producers to channel a until we are going to be done by a function 00:50.960 --> 00:58.180 call producer and the message is read from Channel see are going to be consumed by a process called 00:58.390 --> 00:59.590 consumer. 00:59.590 --> 01:05.380 The funding function will orchestrate all of this meaning that you will read from both tone to lean 01:05.510 --> 01:13.780 on me and send the messages to channel C on the other hand find out that we have a single input channel 01:13.930 --> 01:19.870 in this case channel a and we have a funnel process which given us a certain condition on those messages. 01:21.000 --> 01:26.940 He will find all the messages to either channel me or Chelsea the producer and the consumer. 01:26.940 --> 01:29.350 Semantics remains the same. 01:29.550 --> 01:32.000 So as you can see they do the opposite. 01:32.010 --> 01:38.460 Basically funding reach for many channels and right into a single one and find out reach from a single 01:38.460 --> 01:48.600 channel and write to many others so let's get started by implementing our funding process to start with. 01:48.600 --> 01:55.260 We will need to create a producer which is the function that will send information to a channel so let's 01:55.260 --> 02:01.950 create a function called producer which is going to be receiving as an argument a channel let's call 02:01.950 --> 02:05.620 it C H and it's going to be a channel of pipe into 02:08.690 --> 02:09.400 now. 02:09.410 --> 02:13.570 This is the first concept that I want to introduce which is Channel directions. 02:13.940 --> 02:17.170 We can have a channel defined like this in this channel. 02:17.180 --> 02:24.400 We could send messages and receive messages but if we do it like these we're saying that work is going 02:24.490 --> 02:26.370 to be able to write right this time. 02:26.690 --> 02:29.750 We'll never be able to be on the other side. 02:29.810 --> 02:35.600 If we do like this using the Atlantic and decide rules we're saying that we can just read from the channel 02:36.170 --> 02:37.550 and then try to it. 02:37.550 --> 02:45.950 So in this case the producer will just write the channel soulless to write only town it's also useful 02:45.950 --> 02:51.400 to have a name for the producers so we can differentiate from which producer is sending the messages 02:52.960 --> 02:54.190 OK we have it like this. 02:54.800 --> 03:02.620 So when the producer will do is basically forever loop it will slip some time then we will define the 03:02.620 --> 03:03.560 same function. 03:03.710 --> 03:15.950 So maybe some random time generate a random number on this so this will be an equals to generate randoms. 03:15.970 --> 03:19.580 We will use the math run package. 03:19.790 --> 03:21.970 The math the run package will create. 03:22.230 --> 03:24.800 I would say no. 03:24.980 --> 03:26.150 From zero to 100. 03:26.630 --> 03:27.260 OK. 03:27.350 --> 03:29.000 And then finally we will. 03:29.090 --> 03:33.400 So the message is as simple as doing this right. 03:33.440 --> 03:36.480 We could also bring something to the output. 03:36.800 --> 03:43.750 So let's say Turnell is sending this. 03:43.790 --> 03:48.750 The arguments will be the name of the town and the number but would just share it. 03:49.270 --> 03:51.890 Now we wanted to create a sleep function. 03:51.970 --> 03:59.260 This is going to be sleep is not receiving and returning anything and an hour. 03:59.440 --> 04:05.330 Let's use the time package for these certain duration and the duration is going to be also a random 04:05.330 --> 04:05.810 number. 04:05.810 --> 04:07.980 So let's do random in 04:10.710 --> 04:13.580 again let's say 3 seconds. 04:14.310 --> 04:22.090 So what we're doing cuz we're saying we will sleep for a random amount of time between zero and three 04:22.090 --> 04:28.580 thousand milliseconds which is going to be translating to from zero to three seconds. 04:28.750 --> 04:29.030 OK. 04:30.320 --> 04:33.440 So we have this and now we need to create the chance. 04:33.470 --> 04:43.300 So let's say channel a b a channel of integers and in defining that sample we have three channels. 04:43.310 --> 04:46.720 Channel 8 on B on set on Channel C as well. 04:47.510 --> 04:50.490 So instead of h on the SC. 04:50.820 --> 05:01.790 Now let's go and produce some messages on our aim is gonna be a let's do the same for channel. 05:02.180 --> 05:10.310 So at this point we have two functions two producers sending messages to Channel 8 and 10 B. 05:10.300 --> 05:16.600 And we're using the go keyword to not block in this execution because it's executing forever. 05:17.130 --> 05:24.150 So this is going to continue and then we need to build some consumer and consumer will raise from Tennessee. 05:24.490 --> 05:26.620 OK so let's write the consumer now. 05:26.730 --> 05:31.070 So consumer will receive one channel. 05:31.500 --> 05:37.080 It's going to be a child is but in this case the direction will be a read only child. 05:37.620 --> 05:38.400 Right. 05:38.460 --> 05:44.880 And the consumer function is a simple us forever range over the channel which is basically a reading 05:44.880 --> 05:48.170 from the child and let's print the missiles. 05:48.180 --> 06:00.240 So this will be format based on let's types these the value of the message is going to be like this. 06:00.540 --> 06:06.630 And now wanting to do the orchestration which is different in function and the funding function will 06:06.630 --> 06:14.130 receive a channel a B of input and that's how C S output is the core of our program. 06:14.190 --> 06:16.760 So let's do it like this. 06:17.290 --> 06:25.700 And it will sit a channel a and Channel B which are going to be read only channels. 06:25.800 --> 06:32.400 So there's going to be eight and a channel see which is going to be a right only child. 06:32.460 --> 06:37.660 So there's gonna be Child c of it. 06:38.370 --> 06:38.840 OK. 06:39.010 --> 06:45.680 Now basically the idea is that we will grade from Channel into B and write the content to channel C 06:46.040 --> 06:53.660 so how do we do this on these were we introduce a new console which is select so let's define a variable 06:53.700 --> 06:57.140 call and which is going to hold a value from any time. 06:57.620 --> 07:05.140 And then again let's do a for loop and let's select from either channel a. 07:05.570 --> 07:11.870 So we like this channel a or channel B OK. 07:12.260 --> 07:17.870 Now what we're doing here is we're saying select from many of those channels and read from them. 07:18.140 --> 07:20.380 If you find something do something. 07:20.480 --> 07:27.570 And what we're gonna do is we're going to take the message and forward it to Chelsea. 07:27.940 --> 07:34.540 So the thing is the producer is gonna sleep a certain amount of random time so we might have a case 07:34.540 --> 07:43.990 where Channel a good messages twice and b still don't get any message but he's gonna be changing over 07:43.990 --> 07:44.470 time. 07:44.790 --> 07:47.800 We'll get a better picture when we run this. 07:47.800 --> 07:55.590 So basically it is a defined function it reads from Channel 8 or channel B and sends a message to Chelsea. 07:55.750 --> 08:01.340 If we haven't made any mistake let's execute this. 08:01.410 --> 08:07.140 So this is going to be fun in the sleep. 08:07.190 --> 08:08.240 Apparently it works. 08:08.270 --> 08:11.030 Now let's analyze the output. 08:11.030 --> 08:21.080 What happened is the channel a sends a message here forty seven and the message got grab from the consumer 08:21.470 --> 08:26.820 channel they sent another message to 81 and got read from the consumer as well. 08:26.820 --> 08:32.750 Now Jeremy sends a message and this is what we'll read and so on. 08:33.260 --> 08:39.300 So to summarize channel a channel they send a message and then turn on B this channel a four. 08:39.610 --> 08:47.170 One two three four five times and then turn to B if we execute these again it will probably change. 08:47.230 --> 08:51.770 Going to be like random amount of messages going in and going out. 08:52.230 --> 08:52.640 OK. 08:52.640 --> 08:54.880 So this was fun in. 08:55.040 --> 08:59.410 Now let's do fun out. 08:59.560 --> 09:04.520 To do it we will copy the same code and we will modify a little bit. 09:04.580 --> 09:06.630 So let's do it. 09:06.890 --> 09:10.190 Obviously the funding function is not going to be needed anymore. 09:11.330 --> 09:18.760 And the producer is not going to be named anymore because we have a single producer while we do have 09:18.970 --> 09:22.270 is two different consumers. 09:22.270 --> 09:25.050 So let's name the consumers. 09:25.050 --> 09:27.530 US B and C. 09:27.640 --> 09:28.410 Right. 09:28.570 --> 09:29.390 Sorry. 09:29.470 --> 09:31.900 This is not coming here. 09:31.930 --> 09:33.440 We will do it here. 09:33.460 --> 09:38.000 This is going to be the C consumer and this is going to be the the consumer. 09:38.340 --> 09:41.500 So first of all the producer doesn't receive the streaming anymore. 09:41.500 --> 09:43.920 So we will remove it from here. 09:44.080 --> 09:46.040 The rest is going to be kind of the same. 09:46.120 --> 09:50.680 So we will leave some time to not flood the channels with messages. 09:50.680 --> 09:54.220 We will generate a random number and we will send it. 09:54.250 --> 09:57.380 Now we don't have the name of the producer. 09:57.460 --> 09:59.860 So let's just do this. 10:00.250 --> 10:05.860 Let's just print the message we're sending and then I'll be like this. 10:05.900 --> 10:12.500 Now the consumers we have two consumers 1 per out the town we need to name them in order to make sure 10:12.500 --> 10:16.500 that you know which consumer is reading. 10:16.790 --> 10:18.680 So let's do right. 10:19.370 --> 10:20.900 And the consumers. 10:20.900 --> 10:26.090 It's kind of doing the same but we're just bring up a more useful message. 10:26.180 --> 10:32.920 So let's say the consumer is reading this let's do this. 10:32.920 --> 10:36.180 And again our core function is the one is going to change now. 10:36.440 --> 10:47.780 So let's write it song find out and find out is receiving one input channel Channel a it's going to 10:47.780 --> 10:48.460 be a channel. 10:48.610 --> 10:56.660 I read only channel and throughout the channels it's going to be channel B so C and that type of those 10:56.660 --> 10:58.790 channels is going to be a channel. 10:58.800 --> 11:00.210 Right. 11:00.660 --> 11:03.810 So what is the final channel doing now. 11:03.810 --> 11:08.350 So we've got to read from the Channel 8. 11:09.060 --> 11:14.940 And we need to find some sort of condition let's say if the number we're generating numbers from zero 11:14.940 --> 11:15.450 to 100. 11:15.450 --> 11:15.720 Right. 11:15.750 --> 11:20.310 So let's down the lower half from CEO to 15. 11:21.750 --> 11:28.950 So if the number is below 50 we will send the message to channel the latest. 11:29.250 --> 11:33.510 Otherwise else we will send the message in channel. 11:34.170 --> 11:44.040 So the consumer will should just bring numbers below 50 and see consumer will should just be numbers 11:44.160 --> 11:45.430 both right. 11:46.230 --> 11:54.930 And now let's call the final function and we did this things correctly on those days. 11:55.180 --> 11:56.640 Did you work. 11:56.690 --> 11:58.270 Go find out. 11:59.400 --> 11:59.780 OK. 11:59.820 --> 12:06.660 So with Gerri the number of 87 it was consumed by hope we've made some mistakes. 12:07.060 --> 12:07.440 Yes. 12:07.680 --> 12:09.740 So something is happening here. 12:10.140 --> 12:11.870 Let's try to develop these. 12:11.970 --> 12:14.990 We are producing the messages. 12:15.000 --> 12:17.650 And apparently she has a problem. 12:17.670 --> 12:18.250 Yes. 12:18.300 --> 12:19.860 We're reading from Channel A. 12:20.340 --> 12:27.120 And we're writing the content channel V and Chelsea which seems to be fine. 12:28.110 --> 12:28.630 OK. 12:28.650 --> 12:31.470 This is the problem we're consuming from the same child. 12:31.980 --> 12:38.020 So basically nobody was writing to channel B because nobody was reading from it. 12:38.040 --> 12:39.750 So it wasn't getting fault. 12:39.780 --> 12:40.890 Now it's better. 12:40.920 --> 12:42.120 So let's do it. 12:42.200 --> 12:43.420 What happened now. 12:43.450 --> 12:48.670 Eighty seven is veteran fifty 59 as well the 18 which is below 50. 12:48.680 --> 12:50.110 It's read by B. 12:50.520 --> 12:58.020 So if you see every number below 50 it's handled by Channel B and the other one are handled by channel 12:58.020 --> 12:59.660 C OK. 13:00.600 --> 13:05.450 So this is basically it for funding and find out. 13:05.520 --> 13:13.740 The idea was to kind of mastering the channels into these select statement and also the channel direction 13:13.740 --> 13:16.620 which is super useful to increase the tape safety of them. 13:16.920 --> 13:18.510 So I hope you have enjoyed.