The One BIG Reason to Learn Google's Go Language

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
where did my last video about Google's go language the question I got asked most was why should you learn go maybe you want to learn a new language you know there's python rust C C plus plus C sharp whatever why should you invest your time learning go that's what I want to explain in this video so if you want to find out more please let me explain [Music] of course go like lots of other languages has got all of its kind of nice little features oh that's nice oh you don't need to semicolons well that's good you know all the little things you might look at it and say oh that I like that but there's one big fundamental reason why you should learn go it's not because it's from Google it's not because it's open source it's not because it's being used on you know on lots and lots of websites and servers around the world the one reason is because of the go routines go routines are the building block of go and they are so so useful now hopefully you've had a chance to see my videos here on this channel about how you can use you know fork and P threads kind of an introduction to multi-processing and multitasking I also have another video here on the channel which I really hope you've watched which explains the difference between multi-threading multitasking multi-processing really good background for understanding these terms I also have a video talking about how you can use two cores in Arduino for running two programs separately on a microcontroller in concurrently so that you can kind of get things going on one doing the networking another one doing something else running the UI whatever so I've got several different videos here talking about the idea of concurrency and parallelism now in most languages if you want to use kind of threading there's this kind of big threading API you know the posix P thread API Java's got its own thing you know you can do kind of things at an operating system level let's say with Windows or whatever but the thing about go is it's got this idea of concurrency built right into the language and you can just say turn that into a parallel function run it now concurrently off you go just go and do it and the the language worries about all the stuff that needs to happen to get that to run on the OS that you've compiled for and it's got a whole load of structures for how you can communicate with those routines and for synchronization and so on so on so what I want to do now is it's going to go straight over to the code we're going to write some code we're going to see it running and we're going to look at the idea of a go routine which is a lightweight thread that can run in the background just go do that how you can talk to those background threads uh how you can get results back from them and really that is the key feature of why go is so good because if you're doing any kind of server stuff anything that's running databases email you know mqtt Network stuff whatever it is then be able to scale up to huge numbers of kind of you know these go routines these are absolutely brilliant thing okay let's dive straight into some code okay so here we have a very simple go program it's got one function called say what does it do well there's a loop here zero to less than five so five iterations it will sleep for 100 milliseconds and then it will print out whatever string has been passed in up here so basically it'll print out five times whatever string you pass in slowly and then in the main functions we say say hello and say world so we're going to get five lots of Hello coming out slowly and then five lots of the word world coming out slowly okay let's run it and see that that's what happens so go run say dot go and what do we get hello hello hello hello quite slowly world world world world world quite scary so that's exactly what we expected now of course this whole video is about how you can do go routines which are basically a way of adding parallelism and concurrency to your ah program just by using the inbuilt constructs of go and it's really easy all you do is put the word go in front of the function they want to call and it will create a very lightweight thread and it will go ahead and start running that function separately from the main thread so what we're going to see now if we've done go hello that will start running in the background printing out hello slowly but it will then jump straight to the next function which is to say world and that will start coming out so if things are right the one in the background will just kind of pop out the word hello at the same time as the one saying world is popping out hello so you should get a mixture of hello uh and world so let's just run that and see what happens and here we go that's what we get World hello hello world world hello hello world Wells there you go a mixture of hello world why because the one that we call go in front of it the one that we call go in front is running in the background it runs on its own little thread and starts pumping out those little uh those little words hello on its own little schedule according calls to the schedule of the of the operating system Linux Mac OS Windows whatever okay let's move on to another example okay so what we're going to talk about in this uh little example is a thing called channels now that you've got this little function that can run on its own wouldn't it be great if you could send and receive data from it because if you think about this we're we're trying to build up applications that can do many many things simultaneously and you want to communicate between them so what we've got here is a function called sum and as you can guess here's some at the very end doesn't X Plus y so it adds up two numbers now the way it's going to add up those two numbers it's going to receive them via a thing called a channel and so this here says I want to define the local variable we call the parameters getting the local variable called C is a channel of integers so there are integers coming down this channel and what it says here is the value of y is going to be whatever comes down the channel and you've got this new syntax here this Arrow uh you know is arrow pointing to the left which is really to the left hand side and a minus sign so it cap you know fill up X the data travels in the direction of the arrow so it goes from this channel into X and then the next thing that comes down the channel going from right to left I want to go into y and then actually I want the result of X Plus y to go back down the same channel so channels cut work in both directions so you can receive things from them and you can send things down them now how do you in the main program how do you make the channel where you use the uh make uh keyword and you say it's a channel you can also make slices and Maps if you dig a bit deeper into the go language and as we said earlier it's an integer Channel and then we fire off this little uh program to run in the background now the thing about channels is they are blocking which means it when you say here x is going to be filled with what comes down the channel if there's nothing coming down the channel then it just blocks and it just Waits so what's going to happen here is this little program is going to start this little function and start running in the background then it's going to awake here until it gets something down the channel then it'll wait here until it gets something down the channel and then finally it will send back down the channel the the sum so what do we do is we say call it and then we say well pump down that channel 10 in then pump down the channel 15 so 10 will go into x 15 will go into y it will add them together and then we say well now I want our result to be whatever comes back down the channel so that's what this is here coming down the channel and then we're going to print it out so we should if everything goes right get 25 well let's run it and see what happens so go run chansum.go 25 so that's exactly what we expected to happen and it worked perfectly however there is something we need to watch about these channels just to really understand what's going on when you create the channel obviously the same program here but when you create the channel you can say that the channel has a certain length Now the default lengthy just one which means when you send down a number it reads it into X and it blocks until there's something else that comes down it now if you say that that channel is length two then actually now what's going to happen is 10 and 15 can go into the channel at the same time and then we read back here now which is the tricky thing here when you read back here is it going to be reading the result or if it's running fast enough a raise condition it might read 10 or it might read 15 depending on when it actually gets around to checking what's in the channel so if this line here runs before either of these two lines because there's no way to say you can you can read from the channel that you wrote you can read from the channel you wrote to you don't have to say it has to be another program so in fact if we run this you'll find out it doesn't work so let's run it and see go run Channel sample two ten well that's not the right answer is it 10 plus 15 is not uh 10. so of course what's happened is is that it's put 10 down the channel it's put 15 down the channel and then before this functions actually had a chance to actually read them out here we've read the result back out again so the first thing within the channel was in fact uh 10 so it didn't work so that's a that doesn't actually solve our problem here so if these two things are aren't synchronized then we're going to get some kind of problem now there are different ways of solving this problem the way I've chosen to do it if we look at my next example is to use an input Channel and an output channel so let's have a look again at our sum function here this one now is going to go around forever so it doesn't just exit in these other ones it just exited after it done it's a bit of math here we're going to sit here as a worker thread I'm going to sit here and every time I get to another Channel I'm going to do something with it I'm just going to keep running around here uh heating up the data taking in the data and then once it's all processed you know I'll just sit here and wait for for more and then what we've got here now if you notice we've got an input Channel and output Channel C and C O C for c o for output okay and here I make two Channels One's got six buffer space in it and one's got three why well the two numbers get added up and there's one result so if I can send in six numbers I'll get back three results and then I fire off my little function here which is going to go around in a loop uh taking in these numbers and then printing it and then getting the result and so I I squirt into my input queue all these numbers one two three four five six of them okay and then on the output queue I basically know there's going to be three it's all obviously hard coded you understand that but this just demonstrates how it works uh so I'm going to print out the first result print out the second result and this is coming out of Co the output queue so if we do this we should get 25 130 the sum of these two numbers are some of these two numbers and some of these two numbers and because we're using a different queue for the input a different queue for the output different Channel as they're called in a go then I'm able to synchronize the input and the output one other thing before we run it is that here you I actually print out uh just showing that there's actually the sum being happening and we can also see when it happens in the sequence of things okay let's go ahead and run it and see what happens okay go run there's our program so what's happened we can see that the sum in the background is actually running and it's printing out these three sums as it eats them off the queue and then after that's happened the program schedules actually we read off the the results from the co channel so here's this section here reading off the results so these got fed in pretty quick and they started getting uh processed by this and then once the results started coming out we started reading them uh here before we move on to the next example I do want to remind you you can follow me on Twitter at Gary explained and I also have a monthly newsletter Gary explains.com type email address no spam but you will get the newsletter okay now I've got one final example here which is a bit more complicated and hopefully we'll kind of tie together all of these ideas uh showing that we can have some go functions that are kind of workers that just run in these lightweight threads that come with go and we feed them information and then they do something and we can see how the scheduler picks the next worker Fred that is available so that it can actually do the work so what we're doing here we're going to deal with some prime numbers so the first thing is we're going to send down our Channel no longer just an integer we're actually going to send a little structure here it's all part of the go language that gives you a number that we want to test and a result is it a prime or not so we can send them in and then we can have the results back telling us whether it's prime or not we're going to do it 100 times that's what this one here and how many workers are we going to have we're going to have three of them and we'll show you what that means in a minute okay so here is our function is prime it's basically a trial by a division so it just basically divides up to the square root of the number to see whether it's a prime not necessarily the most efficient way of doing it but good as a proof of concept for us so we have an input Channel an output channel the primes that we want to test will come in here the results will come out here now in each one I'm going to pick a random number between one and a million just so that we can get each time this function is actually running in its own lightweight so I had to print out an ID so we can kind of recognize the different threads that are working this is just for us to understand the the the different go calls are creating different threads with this same function it you wouldn't do this in a production system this is just good for us to understand and all this code does here really just try to divide by every number and see whether it can be divided or not which means it's prime or not and basically we set the false flag or the true flag depending on whether it's prime or not so obey is simple uh Prime testing program as I said not necessarily the most efficient so down here in the main program what do we do well we create a dummy message because we're going to be sending those back and forth okay and so first of all we need to create the channel so there's our two channels and we're going to make sure they have a buffer length so that all the numbers can go into again you might not necessarily want to do this if you're dealing with millions and millions of internet connections database records you know uh mqtt messages whatever it is that you're doing whatever it is you're writing your program then you might not necessarily want to do this way but you wouldn't want to do this you want some kind of buffering that allows you to have some in memory but you don't want all a million Connections in memory uh once but we're only dealing with 100 here so that's what we're going to do okay so the next thing to do is to create the workers these will run in these lightweight threads that are created using the go keyword and it's a for loop it's going to go around num workers time or what's that three times and it's going to call this is prime function as a concurrent routine as a go routine and they'll be sitting there with the input Channel and the output channel so that we can send and receive data from them the next thing we do is fill in that input queue so this goes around test lend time I pick a random number between 1 million and 2 million and I just send that down the channel so now we're filling up this Channel and this channel is going to start to be read by the uh by the is prime function reading them off those channels and once we filled it up we do exactly the same things we did in our previous up we sit here and start to read out the results so we're coming through the output Channel now and we just print out that this number is either true or false depending on on what's happened there's always random numbers so some are going to be and some are not going to be okay so we fill up this queue with 100 numbers between a million and two million the three versions of the is prime function are running and they're going to start taking things off those channels and then putting out their results on the channels okay let's run it and see how that actually works so we want go run Channel Prime now lots of output here let's go back up to the top and see what happened from the very start okay so first of all this number here is that random number that identifies each of the threads so we're expecting three random numbers here and these are the numbers it's testing so there's a whole this one of them is getting a whole bunch of work here look it's doing all that work uh and no one is is contesting it no one is is having a problem so you you go ahead and do that but then suddenly once it's working on that one then a different uh is prime function different go routine starts running and we can see that runs down there and there for what's that one two three four five six seven eight nine items it takes off the channel q and then the other the first one kicks back in again with that same number and it does quite a whole bunch load of them so it's not having a problem doing that and then just at the end here we can see that the other one so two of the cues have bit two of the is prime uh functions have been running there and then we we start to get some of the results coming out so true and false just depending on what they are and then right here at the bottom we get a third one look at this that third go routine it gets a chance to test this last number and the last thing that comes off our queue is that that's false now if we look for this one 1979947 that will be the last one that we see tested up here there it is 1979 and that was tested by our second uh go routine the last one that the primary go routine tested was one one six one five nine eight which I think we'll find right down here at the bottom is it 1161598 is full so there you go three different go routines and they were each taking data off those channels so here is a really really easy way to get concurrency in your programs and this is why you should learn the go language okay that's it the go routine that the major big feature of why you should learn the go programming language do tell me in the comments below if you agree with me is that the killer feature of the go language also please let me know in the comments below if you would like to see more videos on go okay that's it I'll see in the next one
Info
Channel: Gary Explains
Views: 105,878
Rating: undefined out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, Go, Golang, Google Go, Concurrent Programming with Go, Goroutines, Concurrency in Go, Concurrency in Golang, Threads, Channels, Golang and Goroutines, Understanding Golang and Goroutines
Id: rQQcIGqp0OU
Channel Id: undefined
Length: 17min 55sec (1075 seconds)
Published: Wed Nov 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.