Hi in this episode we will learn the fundamentals of the goal and concurrency model namely guarantees
and channels.
First we will do a high level overview of these primitives and then we will implement our example so
to begin let me show you an example.
Let's say we have a process.
This process will spawn a new process thus that's currently in it will wait for this process to end
and then it will exit so in this pound bubble we will create a second process.
This process will do some execution and part of this execution will send a message.
So the way these two process communicate is by sending message and these messages are sand through channels.
So that said this is very simple but it can give us a great power to build concurrent applications so
let us ride this example into go code.
The first thing we have to do is okay.
This is the main process or process wanting the presentation.
So this process will first create a channel.
This is the channel that we will use to communicate.
And this is the syntax make Chan and then the type.
Because remember that everything in goes is typed so let's send integers.
There is an optional argument here which is a number that will tell you the size of this channel.
In this case we won't use any.
So this channel can at most have one message so we have our channel.
And now we need to pound our process to spout a new process.
We used the go keyword and then let's call a function process to let's say now let's create this function.
So process this will receive a channel.
So it's the same type.
This is processed too.
And now let's do this.
Let's wait for up to three seconds.
So this function that what it's doing is creating a random number from 0 to 3000.
Now let's slip here.
This is like we're doing something.
So whereas slipping a random amount of time to pretend we're doing some work duration we're saying that
these are milliseconds OK.
And then what we will do is we're gonna send that number to the channel OK.
So we're creating a random number whereas living for this random amount of time and then we're sending
this number in to the channel using the arrow syntax.
OK.
These are new syntax if we do this we will block until the process is done.
But if we do it with go keyword we are sending this process but will continue our execution.
So let's now print waiting for process and now to read from a channel we should do it like this arrow
and then the channel.
Right.
And then we can easily say uh pre and process 2k milliseconds and it's gonna be okay.
So we have some problems here.
We need to pass the channel because this is the argument.
Okay.
So let's review these ones again before running it.
So first of all we're creating a channel of type and then we're creating a process was pounding a process
called process 2 which is the number of the function and the first argument is the channel itself.
Then we will print some message We will wait for a message in this channel and we will print the content
of the message on the other side we have the process which first creates a random number from 0 to 3000
we sleep for that amount of time pretending we're doing some work.
This might be database operation or an HD TTP request or whatever and then we're sending that message
to the channel.
So now let's execute this.
So we're waiting for the process we're sending the message.
The process took almost two seconds a little bit more.
So the main idea of this is that you can span many process does what you use go keyword for and then
use channels for synchronize this process and communicate them.
So these are the fundamentals of the goal and concurrency model on the next episode.
We will dig into it and build more complex examples.