Ping pong game

4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hi everyone. In this video, I'm going to explain to you a real life example of goroutines and channels by looking at a small ping pong game with two goroutines and two channels. So I want to take, I want you guys to take a look at this piece of code. This has two functions a finger and a panga. So both of these functions take two channels as input. And let's see what they do.

So in this analogy, what happens is we have a finger and we have a panga. So both of these are run as goroutines. And both of these are run indefinitely. So we want both of these to play a ping pong match indefinitely. Now what happens is, the finger waits for the wall gets the ball and then it passes the ball to the Pong while the Palmer is waiting for the ball. When it receives the ball, it passes back back to the Ping.

So if you take a look over here, finger is going to wait for the wait on the pink channel indefinitely. Whenever it receives something or the finger channel. It says ping wait for one second. And send something along the longer channel. Then if you take a look at the longer the longer he waits indefinitely for something along the longer channel, it whenever it gets something it says POM sleeps for just one second and then send something along the finger channel. Now, what essentially is happening is that if you treat something if you treat one as a virtual ball, right, so, what happens first is the finger receives the ball while the Palmer is waiting on the Pong longer channel.

Now, finger receives the ball on the finger channel and then it passes the ball back to the popular channel. So, in this analogy, our channels are like this. Our channel like do arrows, which are like cues. So, moving this one, yeah. So, finger receives the ball On the finger channel, while the bomber is waiting for something to happen when the Bongo receives the ball. When the bomber receives the ball, it says, Okay, I received the ball.

Now I'm going to do something, I'm going to send it back to thing. So now ping receives the ball, while longer is waiting for the ball again, and then ping sends it back on the pongo channel. So in this case, this channel is called the Palmer channel, and this channel is called the ping region. So, taking a look at this code all over again. Now, in the main function, we make two channels, the ping channel and the pump channel. And then we start the two goroutines.

So notice that all of these are infinite loops. But since these infinite loops are happening in the background, then this code is obviously going to run because the function main is asynchronous, these two goroutines main the function asynchrony So we don't have to wait for these two to complete or these two to exit to get to the code over here. So these two are in definitely definitely going to be running in the background and printing to a studio. So, and then we will launch the ball on the pink channel and then we will wait and wait out all the fun. So, if you look at this last piece of code over here, right, so in this case, we are waiting indefinitely so in this case, we can also do a select block with no arguments. So we can wait indefinitely for these two to play a ping pong game.

Because otherwise this function is going to exit because these two go routines are non blocking. So if we quickly do a go run main five.go you see that the two go routines are playing a ping pong game indefinitely. Again, ping is sending on the hunger channel while the bunker is waiting for something The Bongo, when it receives the ball is sending the ball to the finger channel, where ping receives it and then sends it down to the Congress and all over again. So this indefinite loop is what is being implemented by our goal over here. So this is how goroutines and channels really shine. And in the next couple of videos, and in the next section also, we're gonna see a lot of use cases of goroutines and channels, where golang really shines above all other languages.

So stay tuned and see you guys in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.