Golang Channel Basics You MUST Know! | Golang Concurrency EP2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] some ninjas welcome back to the girl like dojo this channel is all about the girl programming language if that's something you fancy and make sure to subscribe unless you are a russ a developer with a tiny pp energy in our last video we talked about go routines and more specifically we talked about how we can spawn multiple processes or go routines in order to do perform multiple tasks at once and one of the problems that we were facing that we weren't able to solve was synchronizing all of these hypotheses at the end in other words we did not have a sufficient means of a communication in order for us to know exactly when to wrap up all of the go routines that we've spawned in today's video that's exactly what we're going to go over and more specifically we are going to utilize this special weapon that golang offers us golang channels in order to solve our dilemma coming up now let's say that we have a two goal routines that will be running in our program the first one is the main goal routine this is the goal routine that will be kicked off immediately after we hit the start button and the main function in our program gets executed the second goal routine is this one this goal team will be kicked off when we use the keyword go follow her by whichever function that we choose to be executed concurrently so the entire program will go something like this we have our main ego routine and our main function that gets executed and starts running a series of instructions like this at some point we will encounter the goalkeeper followed by a function that point we will spawn this new go routine we'll call it the taser go routine and this attached girl routine will also start running a series of instructions like this at the same time though the main goal routine is still running however the problem is that we don't want to maybe the mango routine will finish out of its instructions a little bit earlier than the task go routine does but we don't want to just wrap up the main goal routine right here while we still have a task to go routine running in the while now one way to communicate between these two processes is utilizing something called the sham me access when we are done with this a task process here we will put something in this shared memory maybe we will put like a boolean value a check mark saying that this household routine is done and this mango routine will have some logic to maybe periodically check if there's a share memory location has this check mark if it has a check mark if and only then we will also wrap up the main go routine the main process now as you can probably already guess this means of a communication all of this logic requires you the programmer to write and depending on who you ask this model of a share memory access is not the easiest thing to implement and that is why we have this a powerful tool that we need to utilize in go called channels now let's imagine there is a channel between the two processes i know go figure and before the task routine wraps itself up we first publish this message maybe we would do like boolean value into this channel and only then we will finish up the task routine and this amingo routine will have some way of checking there's a boolean value and only when we gather this message saying that hey the task routine is done will we exit out the main goal within the main function one good thing about the channels is that as the user of the language we don't have to worry about all of the logics to maybe like periodically check if the message is already published into the channel for example and everything is done nicely for us already now let's go ahead and move on into the exciting segment of this video and move all of this logic into actual implementation into actual code with our wonderful ide now here you can see we have some pre-written code here we have an evil ninja his name is tommy and we have a golan ninja mission today and our mission is to take care of this evil ninja name tommy so we would have this a task called attack which will take in this target because it takes some time for us to identify the target and find the perfect timing to actually land these ninja stars at the target we will sleep there's a process of for precisely one second only then we will start throwing ninja stars at this target at the tommy hue now if we were to run this program now you would expect this attacker method to get executed and putting out this line of instruction here but it does not because the main and go routine will finish executing before this attacker method that this attacker go routine will now if we want to do some dummy ways of making this happen we can sleep with the main and go routine for two seconds then we should be able to see this getting printed out here as you can see indeed we are getting this opponent out because the main goal routine is not going to finish itself off for at least two seconds and the attack echo routine will slip only for one second so we know for sure this attacker go routine will finish executing its instructions before the main goal routine will now because it is a golden ninja mission is a time sensitive or we don't want to stay at this evil ninja base for long so we want to also track our performance here so we will have now equals to time dot now and we will have a differ function which will print out the amount of time is spent on this mission we'll go ahead and do time since now and this hopefully will give us a good idea of how much time we spend on this mission and as you can see we are spending about two seconds in this mission and it makes sense because we wouldn't execute out of this main goal routine until two seconds later here however as you can probably already tell we are mission is it taking unnecessarily long to finish because our attack only takes one second and our mission finishes at the two second mark and that is why we can utilize the channels in go in order to wrap this up a little bit quicker so we will go ahead and remove the sleeve two seconds here and use channel maybe we're not gonna call it channel we call it smoke signal is in the ninja world we use the smoke signals to communicate with each other and this will be we use the make a keyword in order to make it this channel and this channel i don't know we'll do like a boolean channel of boolean when we are done we'll just put true into the smoke signal channel that said we will also have to pass in the small signal tool to this attack method here then this smoke signal will or we can do attacked this will be a channel of a boolean after the attack is done we will do true hey the attack is done here and after that we also need to make sure we are getting the message in the main routine here we will do attack so have an error pointing to the left oh it's not the attack it is the smoke signal that we are using here all right so here we have the channel implemented or the smoke signal implemented i go ahead and run this program again as you can see we are only taking one second to finish this entire mission and we are printing out the small signal message right here also a quick note here when we are trying to send messages in this case a boolean value into the channel we will put the arrow pointing to the left on the right hand side of the channel and when we try to receive the message we will put the arrow pointing to the left on the left hand side of this channel variable another thing that is worth noting is that this smoke signal tool can only be used by one single ninja at this point so let's say that we have another ninja in this main go routine instead of the attack it goes into the ninja that is performing the attack in the manga routine that the main ninja also tries to send a message via this smoke signal tool here maybe this time we do false false maybe it will signify that hey we need to get out of this mission we potentially endanger here so if we were to run this program again as you can see it is actually giving us a deadlock and this is not good if we are in a very dangerous machine so we cannot afford deadlocks in situations like this and this can be actually solved by using a buffered channel instead which we'll talk about in our next video so make sure to subscribe into this channel for a future video so that we can properly take care of these evil ninjas such as tommy here without risking the lives of us golang and ninjas and this is how channels work in go if you have any questions make sure to leave them in the comment section down below and make sure to cop your free hair goal and cheat shoot at golangdojo.com make sure to subscribe and i'll see you ninjas in the next video [Music] you
Info
Channel: Golang Dojo
Views: 2,120
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go tutorial, go programming language, golang tutorial for beginners, golang crash course, golang for beginners, golang channels, golang basic channels, golang basic channels tutorial, channels in go tutorial, concurrency in golang, concurrency, thread, golang concurrency, golang thread, golang goroutine, goroutine, golang sync package, go sync package, golang sync, channels in golang
Id: LgCmPHqAuf4
Channel Id: undefined
Length: 10min 41sec (641 seconds)
Published: Wed Aug 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.