Goroutines - Go Programming Tutorial #9
Skills:
AI Workflow Automation70%
Key Takeaways
This video tutorial covers Goroutines in Go programming, a fundamental concept for concurrent programming in the Go language, also known as Golang.
Full Transcript
[Music] what is going on guys welcome back in today's video we're going to talk about concurrent programming in go and go routine so let's get right into it all right so i've mentioned a couple of times before that go is optimized for concurrent programming in today's video we're going to take a look at go routines which are part of concurrent programming uh and for this we're going to start with a very basic example let's say we have a function and this function is called some function and it prints something so fmt dot print line hello world that's it and then we have some other function other function which is going to print something else now what i can do is i can just call these two functions so i can say sum function other function and i can run this program here and you're going to see that i'm going to get hello world something else so what happens if i now sleep in this function so i wait for it i say time sleep which is just basically idle time we're not doing anything we're saying okay wait for two seconds so two times time dot second uh we wait for two seconds and then we print that we're done waiting for example so we print done waiting there you go so what would happen in this case we would print hello world we would wait two seconds and then we would print done waiting then we would call other function which prints something else now if for some reason the order is important because other function depends on some function and they cannot be done in an asynchronous way so it is a problem if other function gets executed before some function or during some function if that is a problem you need to do it in a synchronous way but if it's not a problem if those functions are independent so if i can also call the function while some function is working and it's not a problem we can do it in an asynchronous way now one thing that you need to keep in mind is that asynchronous does not mean multi-threading and it does not mean that we're doing something in parallel it just means that whenever some function or whenever whenever this function here is having idle time i can use this idle time to already start executing the other function uh down here so if i want to do that i just have to call these functions not like that i have to call them in a go routine and i do it by just saying go some function go other function so you're going to see now that if i run this we're seeing [Music] actually we're not seeing anything why is that oh i know why that is because that's another thing that we need to mention if the main function returns it doesn't matter what is still going on if the main function returns it's done so those uh those go routines here are not important so if main reaches the last line of code and returns it doesn't really matter what you write up there uh so if we want to see the results we need to say time sleep 5 for example now this is not a best practice weight this is not a good practice way uh to synchronize stuff so you should never use sleep in order to wait uh because you can await go routines uh you can use stuff like weight groups and go and so on but for now we're just going to use time.sleep and uh by doing that we're just not going to jump out of the main function but if we do that you're going to see that we get hello world uh what do we get then something else and we didn't get we didn't get done waiting why didn't we get done waiting oh i have five times seconds of course that's why time second with a cap less because five is not five seconds five is basically i think milliseconds or microseconds or whatever so if we do it now it should work there you go as you can see we get hello world then we get something else then we get done waiting and then we terminate um this is not the order of the functions because what we do here is we call hello world then hello world or some function actually goes to sleep for two seconds meanwhile other function is using that idle time to print something else and when this waiting is done we're going to print done waiting and then we're going to wait the last three seconds to terminate this program so as you can see this works in an asynchronous way now let's say we have these two functions and they need to share a resource they need to communicate with each other in an asynchronous way so we cannot just have a basic variable because then you don't know okay which one is going to access this variable first and maybe if this variable was not set by one function i cannot receive from it from the other function so we need to do a lot of complicated things if we do it with variables so instead what we can do in go is we can use channels and channels as the name already says are communication channels for the individual go routines so let's create a channel here we do it by saying information for example i'm going to call this channel information it's going to be and we use the make again we we use the make function and we're going to make a channel which is focused on transmitting strings so information is now an information channel through which we can send and receive strings this is the basic idea and now what we can do is we can create actually let's get rid of those two functions here and we're going to function sent information for example we're going to pass a channel here and if we pass a channel this is one thing we also need to keep in mind if we pass a channel to a function we need to specify the direction maybe before we do the function i should show you how the channel works in general um how this basically works is we say information is the channel if you want to send something to the information we do it like that so we basically say information hello world and then if we want to get it out of the information channel for example to print it you say fmt print line and you say arrow information like that so this is how you write into the channel this is how you receive from the channel and what we can do now is we can do this in functions and we can do it concurrently so if we have a function again sent information we need to pass a channel now this channel since we're sending information is going to be directed towards descending and of the channel so we're going to send into the channel uh and we're going to call this channel and in order to specify that we're storing information that we're sending information we need to first put the channel keyword then the arrow and then the data type in this case string so this is basically the parameter for sending into a channel um once we have that we're also going to pass the information piece so just info which is going to be a string and inside of this we're just going to say um channel info like that so this is how you send information to the channel and then we're going to say function print information here we're also going to pass the same channel but we're going to get the other end the receiving end so we're going to say channel and here we're going to have the arrow first then channel and then string and we don't need any second parameter and here we're just going to say fmt dot print line and channel like that so this might seem a little bit complicated but again let me repeat how this works you define a channel information for example you make this channel a string channel and this channel right now is the communication connection between go routines for example um which basically means that if you send into the channel you need to pass it as a sending end you can write information into the channel and the other and the receiving end can get it out from the channel and this happens in an asynchronous way because if there is nothing in the channel and we try to receive it so let's say we don't execute this function but we execute this function this function is to have uh it's going to have a lock it's going to wait and it's going to wait until something enters the channel if nothing enters the channel and i have some stuff down here this stuff is not going to be executed because we are still waiting for the message so it is waiting there until it gets some information which you cannot do in a simple way with ordinary variables because it's just going to read the variable and it's going to notice that nothing is there with a channel it's going to wait for information to enter the channel and it's only going to proceed when information is in the channel and with the default channel we can also not send more stuff into the channel if the channel is already full because we're going to talk about buffer channels in a second but ordinary channels don't have any memory so if you send something to the channel you have to wait until it's received from the channel otherwise you cannot just put more information into the channel so what we can do now is we can go ahead and say go send information [Music] information and then we can say hello then we can say go print information information and we're going to wait obviously down here that's it so let's go ahead and you're going to see we get hello as a result so this also works if we first call the print and then the send so if i go ahead and say go print information information this will also work because we're doing it in an asynchronous way which means that print information is going to try to get from the channel it's going to realize nothing is in the channel so i'm going to wait and this waiting is idle time and in that idle time send information uses the cpu time to store in the channel which basically means okay now there is information in the channel so print information can print that information um if we do this in a non uh or in a synchronized way you're going to see that we get a deadlock probably i think so yeah as you can see we get a deadlock because all go routines are asleep so we're waiting for information to enter the channel and we cannot receive that information now it's the same i think the other way around which might be a little bit surprising um but you can see that the same thing happens if we first send and then print because if we send we cannot send until there is the unless there is a receiving end so the sender is also waiting it's not just sending and storing there but it's actually sending and waiting for someone to receive and unless someone receives this sending threat or descending go routine cannot stop so when you send into a channel you're not putting a value there you're not storing a value there we're sending a value there and someone else has to receive that value so this is the basic way in which channels work and go now all i just explained is true for ordinary channels but not for buffer channels and in go we can also have buffer channels which allow us to store stuff in the channel if no one is receiving so in order to do that we don't say information make change string we say information chan make string comma and then uh the size of the buffer in this case five which basically means we can store five elements in this channel in the buffer of this channel without having someone receiving those five elements uh and then if we add a sixth one then we're going to get a deadlock unless someone is starting to receive stuff so if i go ahead and say information and i'm feeding into the information channel hello world and a bunch of other strings so hello world for example then hi then hello then hey and then i don't know one two three this is going to work so if i run this you're going to see that i'm not going to get a deadlock uh you can see that nothing is happening because we're waiting five seconds but the program is not saying hey i have a deadlock no it's finishing after five seconds after the sleep statement and we're not doing anything in go routines here so we're not doing anything in uh in in an asynchronous way we're just doing this in a very synchronized way um and we can go ahead actually and try to add one more of those lines so i can say information abc for example and then you're going to see that it's going to say deadlock because we're now storing six items or actually we're storing storing five items in the buffer and we're trying to send a sixth item but there's still no receiving end so we're waiting for someone to receive at least one element it's not happening so we have a deadlock however we can go ahead and say dot print line and we can start receiving from the information and we can do so five times and you can see that i get everything from this now if i receive one of those i can go ahead and store another one so i can go ahead and say okay here i can enter abc because i have already taken one out off from the buffer and this is why i can do it now i think i'm not sure to be honest but i think no maybe not i would have to write it in the function doesn't matter so this is how buffer channels work we can have buffers and notice that i'm not doing anything with go routines here this is purely synchronized so this is happening one after the other as you can see i can store five elements then someone starts receiving of course if i have multiple functions running and go routines um we can we can still do it like that the good thing then is of course that i don't have to wait unnecessarily to store stuff into the channel if no one is receiving right now but i know that someone will be receiving eventually so i don't have to unnecessarily wait for receiving end this is how buffer channels work so that's it for today's video hope you enjoyed it hope you'll learn something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Original Description
In this episode we learn about Goroutines in Go.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
🐍 The Python Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Workflow Automation
View skill →Related Reads
📰
📰
📰
📰
AI Tools that Actually Pay You Back: A Developer's Guide to Monetizing AI
Dev.to AI
Creativity AI #82: Anthropic maps how people really use AI, designers shift from making to mending…
Medium · AI
The End of YouTube Search? Why AI Creator Discovery Is Becoming the Smarter Way to Learn in 2026
Medium · AI
Why AI Tools Are Becoming Essential for Modern Professionals
Medium · AI
🎓
Tutor Explanation
DeepCamp AI