Press "Enter" to skip to content

Buffering and Iterating over Channels – Go Lang Practical Programming Tutorial p.23


what’s going on everybody welcome to
part 23 of the go language programming
tutorial series in this video we’re
gonna be building off the last video
where we’ve been talking about go
channels so in the previous tutorial we
just showed a simple example of
basically sending and receiving values
over these channels but we’ve kind of
wondered to kind of run into a few
things and we’re wondering about a few
things the first thing is first of all
we did at least notice that we didn’t
seemingly have to synchronize these
channels at all it appears that they’ve
somehow been able to run and our program
didn’t finish until the values were
there but also we noticed that this is a
little unfortunate because in a simple
toy example sure you could just write
out all the channels that you’re gonna
run but many times you might not know
exactly how many channels you might have
also let’s say you had just new you’re
gonna have a large number maybe a few
hundred maybe a few thousand who knows
you don’t want to have to write out all
these values instead you’d like to be
able to iterate over them so that’s what
we’re going to be covering here talking
about all those things first of all the
first thing that we want to just cover
right out of the gate is by default the
sending and receiving of the go channels
is blocking so when we do basically here
this has to happen before we can attempt
to print and before you know the
function is over so that’s why we didn’t
necessarily have to use any
synchronization but as we’re gonna see
pretty quickly here we’re gonna need to
so so let’s let’s show like a more
realistic example so like rather than
doing it this way let’s say instead
we’re gonna say for I colon equals 0 if
I can type it while I is less than 10 I
just can’t tell you and then we’ll
increment I let’s go ahead and do foo
over foo Val and I so this will be 10 of
them sure we could hard code the 10
receptions of data but that mm that
would be problematic and would begin to
get even worse with a hundred or a
thousand or 10,000 and so on
we’ll just do ten for now though so we
can run those now once we have those how
do we receive them in without having to
hard-code every single value well at
least the one magical thing about go is
the range statement it just can
seemingly range over all kinds of things
so so what we can do is we could say for
item :
equals range foo vowel so we can
actually just range over that foo val
chat channel so however many values it
has awesome we could do format print
line item okay let’s go ahead and run
that and see how we’re doing
so go run go touch-up we have okay so
all at least the first time through
we’re having an issue of all the go
routines being okay so first of all we
need to we need to convert this to be
actual go routine so let’s fix that
let’s go ahead and save that rerun that
it’s not the error I was expecting okay
cool now we’ve got what I was hoping for
so so as you can see we ran it with them
being go routines and actually we got
all the values we wanted if you look at
I mean we definitely ran it where I was
equal to nine right because we got a 45
but then we get this error here all go
routines are asleep deadlock so what’s
happening here well looking here
basically we can see that that basically
like range knows it wants to iterate
over foo Val but there’s really never a
time where we know that foo val is you
know done right so one way that we can
at least finish the the channel is to
initiate a close so we can close
bubele so we could do that and then we
could run this and then we see nothing
happens at all here
so what’s going on there so you might be
thinking hey I’ve seen this before all
we need to do is you synchronize because
basically what’s happening is all the go
routines are you know off running but
then we’re finishing the program before
they come back
right well you’re right that we need to
synchronize things but you’re wrong
about why so to exemplify this what I’ll
do is I’ll just import time and I know
you’d rather me do it in front seas but
it’s just quicker this way I just want
to show an example real quick time dot
sleeve time dot second times two
okay so we’ll add the sleep where this
will kind of confirm or deny whether or
not it’s an issue of the go routines
running so we’ll just go ahead and run
this real quick
and now we actually get to an error and
the error is that we’re panic we’re
trying to send on a closed Channel so
what’s happening here is we’re closing
the channel and we’re actually closing
the channel not reaching the end of the
program we’re closing the channel before
we get to send everything over so like
before these are even done iterating the
channels been closed and boom we’re
trying to send these values but we close
the channel right so yes we do need to
make sure we synchronized but this time
it’s for a slightly different reason
so we’ve done this before so the first
thing that we’re gonna go ahead and do
is well I guess we could have we could
have done the parentheses because we do
need a second import now and that’s
gonna be sync format sync and we know
we’ve done all this before so let’s go
ahead and our wait group and the wait
group will be type sync dot
wait group the other things let’s see
all the things that we need to do first
of all in when since we’re gonna use a
wait group in the actual function itself
in the go routine we need to defer wait
group done we need to make sure that
runs
send over the channel that’s all fine
and dandy each iteration over when we do
like call the go routine to run we need
to wait group dot add one every single
time and then what we want to do is
before we close the channel we need to
do a wait group dot wait so we wait for
all these to basically finish then we
close the channel then we can iterate
over everything so let me close this
lets and then the other thing I’d like
us to do is like right now we’re
blocking so let’s just save this real
quick and then rerun this so we end up
with this just monstrosity of an error
um and what we’d like to do now is use
buffering instead so let me just add in
like a buffer here so foo vowel we know
we have ten items so what we couldn’t do
to add a buffer is just comma and then
whatever the buffer is and these are in
items it’s not bytes so so so ten
basically we want to buffer for ten
items so because basically we don’t want
first of all we don’t want our channels
like normally channels are blocking on
the send and receive and that’s great if
you need it to synchronize your if you
need to synchronize them for whatever
reason in our case though we don’t need
that we have our own form of
synchronization and that’s only gonna
cause those troubles so so we’re gonna
use buffering now they’re not gonna be
blocking and then go run go to and sure
enough finally everything works and we
have our return here so interestingly
enough that almost went in perfect order
there’s a couple it was like really only
one thing that got was different anyway
there we go that’s better anyway so at
this point we can see that we’ve got we
now know how to use go routines how to
synchronize go routines and then also
how to send and receive values over
channels with our go routines so at this
point we’re actually ready to apply
all of this to our our news aggregator
web app which at the moment takes about
five entire seconds to load and our hope
is that we can hopefully get that to be
a much lower number so in the next
tutorial what we’re gonna be doing is
actually applying all this to our
pre-existing web app see how we do and
yeah so if you have questions comments
concerns whatever up to this point feel
free to link below otherwise I will see
you in the next tutorial
Please follow and like us:

Be First to Comment

Leave a Reply