Go Concurrency Explained: Go Routines & Channels

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
concurrency and go is awesome listen I like go you like go we all like go concurrency and go is exceptional due to how easy and lightweight go routines are and the communication mechanism called channels these two tools allow for efficient execution of tasks API calls aggregating data whatever you want you can do it so easy with our built-in tools from go and if you don't know what concurrency is don't worry concept of tasks at the same same time this improves your program efficiency by utilizing all the available resources on your computer and not just one per the execution a good example of this is let's say you have to read a CSV file that has millions of rows you can read each row one by one by one by one by one or you can write a go routine that can read a bunch of rows at once and then move on to the next uncompleted on red row which is so much faster it's gonna save you time so in this video I'm going to show you in detail how you can use go routines and channels in your code with an actual example that calls a real API even though it's free and pulled data from it alright so you can see here I have a very simple main.go file with just one function called Fetch weather and all fetch weather is doing is it's calling the open weather map dot org API by passing in a city which is a string and an API key which I have I know leaked here now again open weather map Returns the temperature for the city that you pass in so it doesn't do anything fancy so let's first declare our list of cities so let's make this an array string and then pass in Toronto let's go with London let's go Paris and let's go finish it off with Tokyo and let's iterate through our cities and pass each City into a fetch weather function so four we can ignore the index City in range cities data is fetch weather City and let's just write for fmt.print Line This is the data passing data like so and this is all we're gonna do however like I said I'm interested in the Benchmark how fast or how slow will these two myths compare awesome so without further Ado let's go ahead and run this so go run main.go and you can see there we are getting the temperatures for our forces for Toronto London Paris and Tokyo we get our temperatures and the total time this operation took is 384 milliseconds and we can run it again just to be sure this one took 406 milliseconds and one last time it took a full second there I think that may have been just something to go with the network or I'm getting stopped but you can see that it's taken roughly between 300 and 400 milliseconds on each request but can we go faster can we do this better so the first thing we want to do is modify our fetch weather function and the first thing I want to do is pass it in a new argument a channel argument it's going to be a string and we're also going to give it a weight group which is going to be a pointer reference to sync dot weight group and the first thing I always like to do when I know a functions will be called in go routines it is put my defer weight group done call here and this is ensures us that will close and send the signal to our primitive which is our weight group to finish off and that this go routine has completed and here right above return data we can do instead use our channels we do fmt dot s print format we can put this is the and here put City now those arguments out of the way let's actually declare them so channel is going to be make Chan string it's going to also says it can read and write to the channel and then our variable weight group is simply going to be sync Dot weight group awesome so now we have to modify our for Loop here and there's a few things we have to do so the first thing is we have to tell our primitive that we're adding a go routine essentially what this is doing for every city in our rib cities we want a separate go routine to function and with four cities in our array that's four separate go routines hence weight group add one method call here instead of calling fetch weather like you do a regular function we have to now pass it into a go routine so what can you do here you can use a keyword go followed by your function name and here instead of City would pass in our Channel and we passed in the reference to our way group and that's all there is that's basically how you run functions and go routines and here you can see we are no longer returning data or explicitly calling fetch weather for our data variable but instead we're going to use a channel to get our data back okay so now there's two more things we need to do the first is actually declaring another go routine I'll explain why in just a second so what this go routine is going to do is actually going to tell our primitive to make sure to wait for all of our go routines to finish so here we have four gold routines but you can have hundreds of go routines running at once it depends on your computer and your server size we need to make sure that all of them complete and once they do we need to make sure that we close our open Channel we do not want to let that thing open for any leakages or anything like that so now maybe ask yourself well milky how can I read the data how can I read the value from my go routine well that's where I'm channels come in so you can do something like for result in range Channel and you can go here fmt.print line result and there you have it now we can execute our fetch weather function in go routines instead of calling them four times sequentially but how much faster is this you may be asking yourself so let's do a go run main.go in the Moment of Truth 233 milliseconds has run that one more time 160 milliseconds like one it one more time 236 which is much much faster than 384 the 406 and even the one second that we got this is a whole different way of executing your calls it's so much faster it's almost two times faster when you organize your code in a way that leverages all the available resources on your computer using concurrency all right so I hope you all enjoyed this video I deep dive into concurrence it's one of the First videos I want to do in this installment of go tutorials if you have any suggestions please let me know in the comment section down below what gold videos do you want what tutorials do you want I know this was a fairly straightforward basic example but I wanted to make it not too advanced I want to make a little intermediate how you could really leverage go routines and channels into your code with a kind of practical practical example with an API call but you can definitely go absolutely crazy with this as way more advanced applications of go routines that you can use but this is just one of the many ways you can leverage and really up your code to be more efficient and as always I need to leave you with two things one is what did you build with your go routine let me know and two oh you got a palette
Info
Channel: Melkey
Views: 60,592
Rating: undefined out of 5
Keywords: video sharing, video, sharing, computer science, software engineer, silicon valley, computer programming, coding, learn to code, machine learning, artificial intelligence, cloud, python, javascript, how to code, Data scientist, AI developer, Machine Learning, golang, go language, golang tutorial, golang concurrency tutorial
Id: B9uR2gLM80E
Channel Id: undefined
Length: 7min 50sec (470 seconds)
Published: Thu Aug 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.