How do channels work in go lang with go routines

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends my name is prima Symbian and today we learn about the channels which is a very important topic and a little bit complex to understand and go so those who have not seen the last video I will just give a quick recap so what we did is now the channel would essentially work with a goroutines so the main purpose of channel is to set up the communication between the go routines like the child routine so as we know that there is a main function which will go sequentially but when you have to do parallelism or concurrency that means you would like to run the programs in different a synchronous process and they'll finish on their own and then they can provide the output so it's non blocking because if you go sequentially like in this example if you go sequentially first thing will finish first then it would wait it would get blocked and second would run and then third something like you know the sequential so what we did is just to make it go routine we put a go keyword there and when we were running this that meet on this so as soon as we run this program it would basically it's not finishing all of them because there is no communication between the main so main spends up the other child goroutines or child processes or threads but as soon as main is done main think okay I am done let me exit so all the other goo routines were unfinished so what's the way to tell mayn't hey wait and till the time you need to communicate with your go routines so this is where the concept of channel comes so what channel would do is it is a link between the main routine and the child go routine so that they can communicate and you can stop it whenever you want like as X so how to establish this we look in the core and then we will understand but the main purpose of channel like and short is to establish the communication between the main routine and the GU routines so how that is done so first thing is let's create a variable called as channel and then we would use the invoke the main main if you have a slice or anything you can initialize with the empty stuff so you can make a channel so you can make a channel of type string so this is how it is done now understand channel you can think that when you go to the toll booth here on a highway with two lane but at time when you know things get slowed then you have the Tollbooth with nine different lanes right and then in the queue you get distributed you pay and then again you connect and go so same thing in a long program you are running sequentially here you're creating different lanes like concurrent lanes or parallel so that they get finished they communicate through the channel and then the message get past but that channel has to be of some specific type it could be string or int or whatever so whenever you are creating that just make sure that whatever the message you are passing in the channel that would be the type now channel you can think of it as a queue so you can put something in the channel at one end and first-in first-out Y at the other end the message would be retrieved so just like the wireless we have a lot of police around and then they communicate through the wireless and then hey everything is okay let that guy go so they let it go otherwise they will hold him there so this is when you create a channel and now you have to receive the message from a channel so you would say you have a message one and then you are assigning value from channel one so this variable so this is a typical keyword which is used if so if you have to put something in the channel you would use this keyword and if you have to retrieve then only you have to use this kind of symbol so before we put it here well let me just print this thing out so we say FM t dot println and message 1 so that everything is all set correct now let's run this so I'm not doing anything here I'm just having this channel and then I am trying to retrieve the message from the channel and printing it but the problem is there is a deadlock and why this deadlock happen is so our normal routine and goo routines they all ran and we were expecting a message from a channel and no one actually put it there and the main kept on waiting in a deadlock because all the routines were finished they were done and nothing came up so that's right executed in abruptly so how would the Guru T know about the channel and how they can convey their message back so we need to make a little bit modification here so this is our go routine like any normal function where you have all normal normal string arguments now you have to pass a channel so you can say C which is of type channel and it accepts trait so now you have a simple you know channel opened up so in the channel you can so you have a message let us create the message so your message is your pass to us it's done now this message you need to put inside a channel so what you would do is you would put it in the sea and that's it so you have accepted a see your channel and you're putting the value in the channel once you are done with your function so as soon as you are done you would say okay so when it is getting in work three times or ten times or how many whatever number of times it would leave the message in the channel and then that message can be retrieved from here so this this guy would wait until and unless it doesn't get the message that is why we were getting this issue last time because we were expecting message and message was not coming now let's fix this so we have to pass the channel as argument in all our functions for our GU routines so now we are all so do you expect this to happen again the deadlock let's see so this time the condition is different we actually pass down the message right but we put three values in the queue for the channel and then we retry only one so this guy was now waiting for other two times like whenever the other two times that GU routine was sending so just to make it simple let's try to do it this way so now we will we are calling a couple of times and then we are reading the message couple of time so the queue is empty everything is all good and then I believe the program should run fine as expected so let's run this and bingo this is normal termination of the program so let me summarize this again so what we did is there is a main routine and then there are Co routines like the child routines so to establish communication what was happening is the once GU routine doesn't know what when the skies are finishing up this will terminate and program might abruptly finish without even getting the output of this so to facilitate this we have the channel so each channel whatever the routine child member routine is done finishing it would send out a message in the channel we established the channel up front so that main passes the channel to the child routine in child routine once it done it acknowledges back to the main so this is how we are acknowledging or sending the message so this symbol whatever you put here so this is a type string then you have to put string in the channel otherwise a number so this here it's a number you are putting it in the channel and you are retrieving it like this you can directly also retrieve it like you don't have to hold it in some you know intermediate variables so this was the quick summary and this was the quick program I hope you got the concept of the GU routines and the channels thanks for watching if you liked the video please like
Info
Channel: Techie Prem Aseem
Views: 1,634
Rating: 4.3684211 out of 5
Keywords: go channels, go routines, go lang channels, channels in go lang
Id: pf4fEYTUzjg
Channel Id: undefined
Length: 10min 12sec (612 seconds)
Published: Sun Aug 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.