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

what’s going on everybody welcome to part 23 of the go language programming

part 23 of the go language programming

part 23 of the go language programming tutorial series in this video we’re

tutorial series in this video we’re

tutorial series in this video we’re gonna be building off the last video

gonna be building off the last video

gonna be building off the last video where we’ve been talking about go

where we’ve been talking about go

where we’ve been talking about go channels so in the previous tutorial we

channels so in the previous tutorial we

channels so in the previous tutorial we just showed a simple example of

just showed a simple example of

just showed a simple example of basically sending and receiving values

basically sending and receiving values

basically sending and receiving values over these channels but we’ve kind of

over these channels but we’ve kind of

over these channels but we’ve kind of wondered to kind of run into a few

wondered to kind of run into a few

wondered to kind of run into a few things and we’re wondering about a few

things and we’re wondering about a few

things and we’re wondering about a few things the first thing is first of all

things the first thing is first of all

things the first thing is first of all we did at least notice that we didn’t

we did at least notice that we didn’t

we did at least notice that we didn’t seemingly have to synchronize these

seemingly have to synchronize these

seemingly have to synchronize these channels at all it appears that they’ve

channels at all it appears that they’ve

channels at all it appears that they’ve somehow been able to run and our program

somehow been able to run and our program

somehow been able to run and our program didn’t finish until the values were

didn’t finish until the values were

didn’t finish until the values were there but also we noticed that this is a

there but also we noticed that this is a

there but also we noticed that this is a little unfortunate because in a simple

little unfortunate because in a simple

little unfortunate because in a simple toy example sure you could just write

toy example sure you could just write

toy example sure you could just write out all the channels that you’re gonna

out all the channels that you’re gonna

out all the channels that you’re gonna run but many times you might not know

run but many times you might not know

run but many times you might not know exactly how many channels you might have

exactly how many channels you might have

exactly how many channels you might have also let’s say you had just new you’re

also let’s say you had just new you’re

also let’s say you had just new you’re gonna have a large number maybe a few

gonna have a large number maybe a few

gonna have a large number maybe a few hundred maybe a few thousand who knows

hundred maybe a few thousand who knows

hundred maybe a few thousand who knows you don’t want to have to write out all

you don’t want to have to write out all

you don’t want to have to write out all these values instead you’d like to be

these values instead you’d like to be

these values instead you’d like to be able to iterate over them so that’s what

able to iterate over them so that’s what

able to iterate over them so that’s what we’re going to be covering here talking

we’re going to be covering here talking

we’re going to be covering here talking about all those things first of all the

about all those things first of all the

about all those things first of all the first thing that we want to just cover

first thing that we want to just cover

first thing that we want to just cover right out of the gate is by default the

right out of the gate is by default the

right out of the gate is by default the sending and receiving of the go channels

sending and receiving of the go channels

sending and receiving of the go channels is blocking so when we do basically here

is blocking so when we do basically here

is blocking so when we do basically here this has to happen before we can attempt

this has to happen before we can attempt

this has to happen before we can attempt to print and before you know the

to print and before you know the

to print and before you know the function is over so that’s why we didn’t

function is over so that’s why we didn’t

function is over so that’s why we didn’t necessarily have to use any

necessarily have to use any

necessarily have to use any synchronization but as we’re gonna see

synchronization but as we’re gonna see

synchronization but as we’re gonna see pretty quickly here we’re gonna need to

pretty quickly here we’re gonna need to

pretty quickly here we’re gonna need to so so let’s let’s show like a more

so so let’s let’s show like a more

so so let’s let’s show like a more realistic example so like rather than

realistic example so like rather than

realistic example so like rather than doing it this way let’s say instead

doing it this way let’s say instead

doing it this way let’s say instead we’re gonna say for I colon equals 0 if

we’re gonna say for I colon equals 0 if

we’re gonna say for I colon equals 0 if I can type it while I is less than 10 I

I can type it while I is less than 10 I

I can type it while I is less than 10 I just can’t tell you and then we’ll

just can’t tell you and then we’ll

just can’t tell you and then we’ll increment I let’s go ahead and do foo

increment I let’s go ahead and do foo

increment I let’s go ahead and do foo over foo Val and I so this will be 10 of

over foo Val and I so this will be 10 of

over foo Val and I so this will be 10 of them sure we could hard code the 10

them sure we could hard code the 10

them sure we could hard code the 10 receptions of data but that mm that

receptions of data but that mm that

receptions of data but that mm that would be problematic and would begin to

would be problematic and would begin to

would be problematic and would begin to get even worse with a hundred or a

get even worse with a hundred or a

get even worse with a hundred or a thousand or 10,000 and so on

thousand or 10,000 and so on

thousand or 10,000 and so on we’ll just do ten for now though so we

we’ll just do ten for now though so we

we’ll just do ten for now though so we can run those now once we have those how

can run those now once we have those how

can run those now once we have those how do we receive them in without having to

do we receive them in without having to

do we receive them in without having to hard-code every single value well at

hard-code every single value well at

hard-code every single value well at least the one magical thing about go is

least the one magical thing about go is

least the one magical thing about go is the range statement it just can

the range statement it just can

the range statement it just can seemingly range over all kinds of things

seemingly range over all kinds of things

seemingly range over all kinds of things so so what we can do is we could say for

so so what we can do is we could say for

so so what we can do is we could say for item :

item :

item : equals range foo vowel so we can

equals range foo vowel so we can

equals range foo vowel so we can actually just range over that foo val

actually just range over that foo val

actually just range over that foo val chat channel so however many values it

chat channel so however many values it

chat channel so however many values it has awesome we could do format print

has awesome we could do format print

has awesome we could do format print line item okay let’s go ahead and run

line item okay let’s go ahead and run

line item okay let’s go ahead and run that and see how we’re doing

that and see how we’re doing

that and see how we’re doing so go run go touch-up we have okay so

so go run go touch-up we have okay so

so go run go touch-up we have okay so all at least the first time through

all at least the first time through

all at least the first time through we’re having an issue of all the go

we’re having an issue of all the go

we’re having an issue of all the go routines being okay so first of all we

routines being okay so first of all we

routines being okay so first of all we need to we need to convert this to be

need to we need to convert this to be

need to we need to convert this to be actual go routine so let’s fix that

actual go routine so let’s fix that

actual go routine so let’s fix that let’s go ahead and save that rerun that

let’s go ahead and save that rerun that

let’s go ahead and save that rerun that it’s not the error I was expecting okay

it’s not the error I was expecting okay

it’s not the error I was expecting okay cool now we’ve got what I was hoping for

cool now we’ve got what I was hoping for

cool now we’ve got what I was hoping for so so as you can see we ran it with them

so so as you can see we ran it with them

so so as you can see we ran it with them being go routines and actually we got

being go routines and actually we got

being go routines and actually we got all the values we wanted if you look at

all the values we wanted if you look at

all the values we wanted if you look at I mean we definitely ran it where I was

I mean we definitely ran it where I was

I mean we definitely ran it where I was equal to nine right because we got a 45

equal to nine right because we got a 45

equal to nine right because we got a 45 but then we get this error here all go

but then we get this error here all go

but then we get this error here all go routines are asleep deadlock so what’s

routines are asleep deadlock so what’s

routines are asleep deadlock so what’s happening here well looking here

happening here well looking here

happening here well looking here basically we can see that that basically

basically we can see that that basically

basically we can see that that basically like range knows it wants to iterate

like range knows it wants to iterate

like range knows it wants to iterate over foo Val but there’s really never a

over foo Val but there’s really never a

over foo Val but there’s really never a time where we know that foo val is you

time where we know that foo val is you

time where we know that foo val is you know done right so one way that we can

know done right so one way that we can

know done right so one way that we can at least finish the the channel is to

at least finish the the channel is to

at least finish the the channel is to initiate a close so we can close

initiate a close so we can close

initiate a close so we can close bubele so we could do that and then we

bubele so we could do that and then we

bubele so we could do that and then we could run this and then we see nothing

could run this and then we see nothing

could run this and then we see nothing happens at all here

happens at all here

happens at all here so what’s going on there so you might be

so what’s going on there so you might be

so what’s going on there so you might be thinking hey I’ve seen this before all

thinking hey I’ve seen this before all

thinking hey I’ve seen this before all we need to do is you synchronize because

we need to do is you synchronize because

we need to do is you synchronize because basically what’s happening is all the go

basically what’s happening is all the go

basically what’s happening is all the go routines are you know off running but

routines are you know off running but

routines are you know off running but then we’re finishing the program before

then we’re finishing the program before

then we’re finishing the program before they come back

they come back

they come back right well you’re right that we need to

right well you’re right that we need to

right well you’re right that we need to synchronize things but you’re wrong

synchronize things but you’re wrong

synchronize things but you’re wrong about why so to exemplify this what I’ll

about why so to exemplify this what I’ll

about why so to exemplify this what I’ll do is I’ll just import time and I know

do is I’ll just import time and I know

do is I’ll just import time and I know you’d rather me do it in front seas but

you’d rather me do it in front seas but

you’d rather me do it in front seas but it’s just quicker this way I just want

it’s just quicker this way I just want

it’s just quicker this way I just want to show an example real quick time dot

to show an example real quick time dot

to show an example real quick time dot sleeve time dot second times two

sleeve time dot second times two

sleeve time dot second times two okay so we’ll add the sleep where this

okay so we’ll add the sleep where this

okay so we’ll add the sleep where this will kind of confirm or deny whether or

will kind of confirm or deny whether or

will kind of confirm or deny whether or not it’s an issue of the go routines

not it’s an issue of the go routines

not it’s an issue of the go routines running so we’ll just go ahead and run

running so we’ll just go ahead and run

running so we’ll just go ahead and run this real quick

this real quick

this real quick and now we actually get to an error and

and now we actually get to an error and

and now we actually get to an error and the error is that we’re panic we’re

the error is that we’re panic we’re

the error is that we’re panic we’re trying to send on a closed Channel so

trying to send on a closed Channel so

trying to send on a closed Channel so what’s happening here is we’re closing

what’s happening here is we’re closing

what’s happening here is we’re closing the channel and we’re actually closing

the channel and we’re actually closing

the channel and we’re actually closing the channel not reaching the end of the

the channel not reaching the end of the

the channel not reaching the end of the program we’re closing the channel before

program we’re closing the channel before

program we’re closing the channel before we get to send everything over so like

we get to send everything over so like

we get to send everything over so like before these are even done iterating the

before these are even done iterating the

before these are even done iterating the channels been closed and boom we’re

channels been closed and boom we’re

channels been closed and boom we’re trying to send these values but we close

trying to send these values but we close

trying to send these values but we close the channel right so yes we do need to

the channel right so yes we do need to

the channel right so yes we do need to make sure we synchronized but this time

make sure we synchronized but this time

make sure we synchronized but this time it’s for a slightly different reason

it’s for a slightly different reason

it’s for a slightly different reason so we’ve done this before so the first

so we’ve done this before so the first

so we’ve done this before so the first thing that we’re gonna go ahead and do

thing that we’re gonna go ahead and do

thing that we’re gonna go ahead and do is well I guess we could have we could

is well I guess we could have we could

is well I guess we could have we could have done the parentheses because we do

have done the parentheses because we do

have done the parentheses because we do need a second import now and that’s

need a second import now and that’s

need a second import now and that’s gonna be sync format sync and we know

gonna be sync format sync and we know

gonna be sync format sync and we know we’ve done all this before so let’s go

we’ve done all this before so let’s go

we’ve done all this before so let’s go ahead and our wait group and the wait

ahead and our wait group and the wait

ahead and our wait group and the wait group will be type sync dot

group will be type sync dot

group will be type sync dot wait group the other things let’s see

wait group the other things let’s see

wait group the other things let’s see all the things that we need to do first

all the things that we need to do first

all the things that we need to do first of all in when since we’re gonna use a

of all in when since we’re gonna use a

of all in when since we’re gonna use a wait group in the actual function itself

wait group in the actual function itself

wait group in the actual function itself in the go routine we need to defer wait

in the go routine we need to defer wait

in the go routine we need to defer wait group done we need to make sure that

group done we need to make sure that

group done we need to make sure that runs

runs

runs send over the channel that’s all fine

send over the channel that’s all fine

send over the channel that’s all fine and dandy each iteration over when we do

and dandy each iteration over when we do

and dandy each iteration over when we do like call the go routine to run we need

like call the go routine to run we need

like call the go routine to run we need to wait group dot add one every single

to wait group dot add one every single

to wait group dot add one every single time and then what we want to do is

time and then what we want to do is

time and then what we want to do is before we close the channel we need to

before we close the channel we need to

before we close the channel we need to do a wait group dot wait so we wait for

do a wait group dot wait so we wait for

do a wait group dot wait so we wait for all these to basically finish then we

all these to basically finish then we

all these to basically finish then we close the channel then we can iterate

close the channel then we can iterate

close the channel then we can iterate over everything so let me close this

over everything so let me close this

over everything so let me close this lets and then the other thing I’d like

lets and then the other thing I’d like

lets and then the other thing I’d like us to do is like right now we’re

us to do is like right now we’re

us to do is like right now we’re blocking so let’s just save this real

blocking so let’s just save this real

blocking so let’s just save this real quick and then rerun this so we end up

quick and then rerun this so we end up

quick and then rerun this so we end up with this just monstrosity of an error

with this just monstrosity of an error

with this just monstrosity of an error um and what we’d like to do now is use

um and what we’d like to do now is use

um and what we’d like to do now is use buffering instead so let me just add in

buffering instead so let me just add in

buffering instead so let me just add in like a buffer here so foo vowel we know

like a buffer here so foo vowel we know

like a buffer here so foo vowel we know we have ten items so what we couldn’t do

we have ten items so what we couldn’t do

we have ten items so what we couldn’t do to add a buffer is just comma and then

to add a buffer is just comma and then

to add a buffer is just comma and then whatever the buffer is and these are in

whatever the buffer is and these are in

whatever the buffer is and these are in items it’s not bytes so so so ten

items it’s not bytes so so so ten

items it’s not bytes so so so ten basically we want to buffer for ten

basically we want to buffer for ten

basically we want to buffer for ten items so because basically we don’t want

items so because basically we don’t want

items so because basically we don’t want first of all we don’t want our channels

first of all we don’t want our channels

first of all we don’t want our channels like normally channels are blocking on

like normally channels are blocking on

like normally channels are blocking on the send and receive and that’s great if

the send and receive and that’s great if

the send and receive and that’s great if you need it to synchronize your if you

you need it to synchronize your if you

you need it to synchronize your if you need to synchronize them for whatever

need to synchronize them for whatever

need to synchronize them for whatever reason in our case though we don’t need

reason in our case though we don’t need

reason in our case though we don’t need that we have our own form of

that we have our own form of

that we have our own form of synchronization and that’s only gonna

synchronization and that’s only gonna

synchronization and that’s only gonna cause those troubles so so we’re gonna

cause those troubles so so we’re gonna

cause those troubles so so we’re gonna use buffering now they’re not gonna be

use buffering now they’re not gonna be

use buffering now they’re not gonna be blocking and then go run go to and sure

blocking and then go run go to and sure

blocking and then go run go to and sure enough finally everything works and we

enough finally everything works and we

enough finally everything works and we have our return here so interestingly

have our return here so interestingly

have our return here so interestingly enough that almost went in perfect order

enough that almost went in perfect order

enough that almost went in perfect order there’s a couple it was like really only

there’s a couple it was like really only

there’s a couple it was like really only one thing that got was different anyway

one thing that got was different anyway

one thing that got was different anyway there we go that’s better anyway so at

there we go that’s better anyway so at

there we go that’s better anyway so at this point we can see that we’ve got we

this point we can see that we’ve got we

this point we can see that we’ve got we now know how to use go routines how to

now know how to use go routines how to

now know how to use go routines how to synchronize go routines and then also

synchronize go routines and then also

synchronize go routines and then also how to send and receive values over

how to send and receive values over

how to send and receive values over channels with our go routines so at this

channels with our go routines so at this

channels with our go routines so at this point we’re actually ready to apply

point we’re actually ready to apply

point we’re actually ready to apply all of this to our our news aggregator

all of this to our our news aggregator

all of this to our our news aggregator web app which at the moment takes about

web app which at the moment takes about

web app which at the moment takes about five entire seconds to load and our hope

five entire seconds to load and our hope

five entire seconds to load and our hope is that we can hopefully get that to be

is that we can hopefully get that to be

is that we can hopefully get that to be a much lower number so in the next

a much lower number so in the next

a much lower number so in the next tutorial what we’re gonna be doing is

tutorial what we’re gonna be doing is

tutorial what we’re gonna be doing is actually applying all this to our

actually applying all this to our

actually applying all this to our pre-existing web app see how we do and

pre-existing web app see how we do and

pre-existing web app see how we do and yeah so if you have questions comments

yeah so if you have questions comments

yeah so if you have questions comments concerns whatever up to this point feel

concerns whatever up to this point feel

concerns whatever up to this point feel free to link below otherwise I will see

free to link below otherwise I will see

free to link below otherwise I will see you in the next tutorial

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *