Press "Enter" to skip to content

Goroutines – Go Lang Practical Programming Tutorial p.18


what’s going on everybody welcome to
another go language programming tutorial
video in this video what we’re gonna be
talking about is concurrency or at least
we’re gonna start talking about
concurrency in the go programming
language now first what we should talk
about is what exactly is concurrency
because this can be kind of confusing
so concurrency is not in parallel the
way you can think about it is
concurrency is like dealing with
multiple things at any one time
whereas parallel parallelism is doing
multiple things at any one time so for
example concurrency is like consider
your like a CEO of a company you’re
writing a company-wide memo but as
emails come in you’re responding emails
maybe someone comes to your door and
asks a quick question you you pause the
memo and you answer that question and
then you get back to the memo something
like that you’re doing those things
concurrently so that way when that
person comes to your door they don’t
have to like stand there until you’re
finished with the memo necessarily or
something like that so in our case like
with our web app we’re spending a lot of
time just sitting there we’re sending
out the requests we’re waiting for the
requests to come back that’s a lot at a
time it’s like 50 to 75 milliseconds
multiply that by every single site map
that we’re visiting and they just keep
adding up whereas we actually could be
while we’re waiting for one thing to
make a response we actually could send
out another request or waiting for that
also to make a response from sound on
another request chances are we could
probably send out all our requests in 75
milliseconds so so that’s that’s
concurrency so in go the way we do
concurrency is with go routines the way
we use a go routine is you just simply
type go I have no idea where it is but
anyways type go in front of the function
call so let’s just make a quick example
to illustrate this so I’m just going to
say package main and then we’re gonna go
ahead and just we’re gonna make a couple
imports we’re going to import time and
format just to print things out and then
let’s say we’ve got a function that says
whatever string we pass and that’s all
it does it just says whatever so in this
case and in fact let’s make it a little
more complicated so let’s say
iterates like it says the thing like
three times so let’s say for I : equals
zero while I is less than three and then
I increment let’s just format dot print
line s and then also let’s go ahead and
do a time die capital s sleep time diet
millisecond times 100 so okay cool I
mean this should be totally clear like
what’s going on in this function so
hopefully you understand now let’s do it
go ahead and make our main and then our
main function
if we will like let’s say we’re gonna
say two things let’s say we’re gonna say
say hey and then say there so let’s save
that come up here and then go run go go
and we see hey hey hey
there there there everything kind of ran
you know in parallel here or not
parallel
ran linearly rather so when we got to
this it ran each of the iterations
before it got to this line now instead
what if we converted one of these things
to a goroutine
for example let’s just convert the first
one to a go routine so we just throw go
the ghost statement right in front of
the function that’s it so a go routine
and go is just like a lightweight thread
okay so it’s gonna launch say hey in a
thread for us and that’s it that’s all
you have to do to to it so let’s go
ahead and run that really quick and here
we can see it’s just okay this time it
actually said there before hey I won’t
necessarily I don’t think it wouldn’t
necessarily always do that but anyway it
might have maybe it would actually for a
reason I’m gonna explain in a moment so
what if we made both of these go regions
so we say go say hey go say there so
what if we made them both go routines
what do you what would happen so let’s
go ahead and run it real quick and
suddenly we see nothing what if what if
we said
like let’s say we do say hey and then go
say there so the first example we did
was go say hey and then we just said say
there this time what I want us to do is
to do say hey and then do the go say
there so what’s gonna happen here is it
just says hey hey hey right and so and
if we made again both of these go
routines whoops if we made them both go
routines where is we get nothing so
what’s going on here
well the go routine is just kind of
launched out but it’s gonna it’s running
concurrently there’s nothing that that
says hey this absolutely must finish
right so if your program finishes before
the go routine finishes it’s over it’s
it’s non-blocking so it’s just not gonna
do anything so even though we we asked
these things to run they’re just not
getting done that’s why when we said go
say hey and then say there well this
requires required to finish so it was
definitely gonna finish and that’s why
this was able to actually run and then I
guess just the launching of making this
concurrent took a little bit like longer
and then we actually got to this point
and then that’s why pride there is
coming first not positive that’s just my
guess so instead let’s say you said go
say hey go say there we can kind of
illustrate that that’s the only issue
that we’re having by doing a time not
sleep time diet let’s just do a second
save that come up here and we can run
this and sure enough everything runs it
waits for a second in fact it just waits
for a second right because these are
just go routines so these launched
launched it’s not actually waiting for
these and then it sleeps for a second
before the program ends so anyway so
those are that’s just a really quick
basic example of go routines but of
course immediately you’re probably
wondering well I mean that’s all fine
and dandy and like a simple example
where you just kind of add a sleep at
the end and that’s all you got to do but
in practice this is not a good idea
right like you don’t want to
handle for your go routines bye-bye
thinking to yourself okay how long about
should these take and then you know
maybe I’ll just add a little more time
and then that’s fine well cuz you’re
gonna leave time on the table so surely
there’s a better way right and there is
and so in the next tutorial what we’re
really talking about is how you can
actually synchronize these go routines
to wait for them to complete and to
basically basically wait for them to
complete without leaving time on the
table using something arbitrary and
hacky like time not sleep for some
amount of time so anyways that’s what
we’re gonna be dealing with in the next
tutorial if you have questions comments
concerns whatever feel free to leave
them below otherwise I will see you in
the next tutorial
Please follow and like us:

Be First to Comment

Leave a Reply