WEBVTT 00:06.480 --> 00:13.050 Hi in this episode we will learn the fundamentals of the goal and concurrency model namely guarantees 00:13.130 --> 00:14.020 and channels. 00:14.740 --> 00:22.750 First we will do a high level overview of these primitives and then we will implement our example so 00:22.810 --> 00:25.960 to begin let me show you an example. 00:26.020 --> 00:28.360 Let's say we have a process. 00:28.360 --> 00:36.310 This process will spawn a new process thus that's currently in it will wait for this process to end 00:36.790 --> 00:45.970 and then it will exit so in this pound bubble we will create a second process. 00:45.970 --> 00:52.710 This process will do some execution and part of this execution will send a message. 00:52.780 --> 01:01.940 So the way these two process communicate is by sending message and these messages are sand through channels. 01:02.560 --> 01:10.470 So that said this is very simple but it can give us a great power to build concurrent applications so 01:10.470 --> 01:15.590 let us ride this example into go code. 01:15.620 --> 01:19.990 The first thing we have to do is okay. 01:20.230 --> 01:23.410 This is the main process or process wanting the presentation. 01:23.420 --> 01:27.580 So this process will first create a channel. 01:27.590 --> 01:30.500 This is the channel that we will use to communicate. 01:30.740 --> 01:33.840 And this is the syntax make Chan and then the type. 01:33.860 --> 01:39.740 Because remember that everything in goes is typed so let's send integers. 01:39.890 --> 01:46.700 There is an optional argument here which is a number that will tell you the size of this channel. 01:46.700 --> 01:48.640 In this case we won't use any. 01:48.650 --> 01:54.990 So this channel can at most have one message so we have our channel. 01:54.990 --> 01:59.310 And now we need to pound our process to spout a new process. 01:59.310 --> 02:09.350 We used the go keyword and then let's call a function process to let's say now let's create this function. 02:09.340 --> 02:14.860 So process this will receive a channel. 02:15.020 --> 02:17.160 So it's the same type. 02:17.180 --> 02:18.280 This is processed too. 02:18.890 --> 02:20.770 And now let's do this. 02:20.780 --> 02:26.090 Let's wait for up to three seconds. 02:26.090 --> 02:33.860 So this function that what it's doing is creating a random number from 0 to 3000. 02:33.890 --> 02:36.160 Now let's slip here. 02:36.200 --> 02:37.750 This is like we're doing something. 02:37.790 --> 02:46.800 So whereas slipping a random amount of time to pretend we're doing some work duration we're saying that 02:46.800 --> 02:50.190 these are milliseconds OK. 02:50.740 --> 02:57.070 And then what we will do is we're gonna send that number to the channel OK. 02:57.330 --> 03:03.150 So we're creating a random number whereas living for this random amount of time and then we're sending 03:03.240 --> 03:07.570 this number in to the channel using the arrow syntax. 03:07.590 --> 03:08.000 OK. 03:07.990 --> 03:14.260 These are new syntax if we do this we will block until the process is done. 03:14.430 --> 03:20.790 But if we do it with go keyword we are sending this process but will continue our execution. 03:20.850 --> 03:31.880 So let's now print waiting for process and now to read from a channel we should do it like this arrow 03:32.680 --> 03:33.940 and then the channel. 03:34.030 --> 03:34.780 Right. 03:34.840 --> 03:43.280 And then we can easily say uh pre and process 2k milliseconds and it's gonna be okay. 03:43.990 --> 03:46.190 So we have some problems here. 03:46.420 --> 03:49.700 We need to pass the channel because this is the argument. 03:49.720 --> 03:50.140 Okay. 03:50.590 --> 03:54.430 So let's review these ones again before running it. 03:54.430 --> 04:01.960 So first of all we're creating a channel of type and then we're creating a process was pounding a process 04:01.990 --> 04:07.090 called process 2 which is the number of the function and the first argument is the channel itself. 04:07.090 --> 04:12.940 Then we will print some message We will wait for a message in this channel and we will print the content 04:12.940 --> 04:20.530 of the message on the other side we have the process which first creates a random number from 0 to 3000 04:21.860 --> 04:25.700 we sleep for that amount of time pretending we're doing some work. 04:25.700 --> 04:32.180 This might be database operation or an HD TTP request or whatever and then we're sending that message 04:32.780 --> 04:33.380 to the channel. 04:33.530 --> 04:35.680 So now let's execute this. 04:35.750 --> 04:39.470 So we're waiting for the process we're sending the message. 04:39.470 --> 04:43.680 The process took almost two seconds a little bit more. 04:44.020 --> 04:52.750 So the main idea of this is that you can span many process does what you use go keyword for and then 04:52.750 --> 04:57.110 use channels for synchronize this process and communicate them. 04:57.130 --> 05:02.410 So these are the fundamentals of the goal and concurrency model on the next episode. 05:02.410 --> 05:05.470 We will dig into it and build more complex examples.