Press "Enter" to skip to content

Channels – Go Lang Practical Programming Tutorial p.22


what is going on everybody and welcome

what is going on everybody and welcome to part 22 of the Golan tutorial series

to part 22 of the Golan tutorial series

to part 22 of the Golan tutorial series in this tutorial what we’re talking

in this tutorial what we’re talking

in this tutorial what we’re talking about is channels so the idea of

about is channels so the idea of

about is channels so the idea of channels with go is to use them with

channels with go is to use them with

channels with go is to use them with your go routines in order to send and

your go routines in order to send and

your go routines in order to send and receive values between them using the

receive values between them using the

receive values between them using the channel operator and the channel

channel operator and the channel

channel operator and the channel operator is just your less than sign and

operator is just your less than sign and

operator is just your less than sign and then a minus sign basically it looks

then a minus sign basically it looks

then a minus sign basically it looks like an arrow so let’s go ahead and just

like an arrow so let’s go ahead and just

like an arrow so let’s go ahead and just start with a really I don’t know what I

start with a really I don’t know what I

start with a really I don’t know what I was about to type but anyway package

was about to type but anyway package

was about to type but anyway package main will import format will also spell

main will import format will also spell

main will import format will also spell import right and then won’t have func

import right and then won’t have func

import right and then won’t have func main and and what we’ll do first is we

main and and what we’ll do first is we

main and and what we’ll do first is we should let’s make a channel so generally

should let’s make a channel so generally

should let’s make a channel so generally to make a channel you’re gonna have make

to make a channel you’re gonna have make

to make a channel you’re gonna have make and then you’ll have the the channel so

and then you’ll have the the channel so

and then you’ll have the the channel so we’re gonna make a Chan and then you

we’re gonna make a Chan and then you

we’re gonna make a Chan and then you give the room the type of the channel so

give the room the type of the channel so

give the room the type of the channel so in this case it’s going to be type int

in this case it’s going to be type int

in this case it’s going to be type int so we can assign this and give this a

so we can assign this and give this a

so we can assign this and give this a you know to a vat variable by doing foo

you know to a vat variable by doing foo

you know to a vat variable by doing foo Val

Val

Val so foo val is a a channel of the int

so foo val is a a channel of the int

so foo val is a a channel of the int type now of course just like a lot of

type now of course just like a lot of

type now of course just like a lot of things in go if you wanted that channel

things in go if you wanted that channel

things in go if you wanted that channel it could contain a type that you created

it could contain a type that you created

it could contain a type that you created with a struct so if you wanted to pass

with a struct so if you wanted to pass

with a struct so if you wanted to pass be able to pass you know every time an

be able to pass you know every time an

be able to pass you know every time an integer a string and then a float or

integer a string and then a float or

integer a string and then a float or something like that you could create a

something like that you could create a

something like that you could create a custom type and pass that instead so

custom type and pass that instead so

custom type and pass that instead so anyways our channel will be foo fowl and

anyways our channel will be foo fowl and

anyways our channel will be foo fowl and then we’re going to do is we’re gonna

then we’re going to do is we’re gonna

then we’re going to do is we’re gonna come up here and we’re gonna create func

come up here and we’re gonna create func

come up here and we’re gonna create func foo and basically what we’re gonna do

foo and basically what we’re gonna do

foo and basically what we’re gonna do with foo is let’s just say we’re going

with foo is let’s just say we’re going

with foo is let’s just say we’re going to take a value so we’re gonna take you

to take a value so we’re gonna take you

to take a value so we’re gonna take you know some value and then we want to

know some value and then we want to

know some value and then we want to multiply it by 5 so this foo function

multiply it by 5 so this foo function

multiply it by 5 so this foo function needs to at least accept in some value

needs to at least accept in some value

needs to at least accept in some value and that’ll be of the int type now if we

and that’ll be of the int type now if we

and that’ll be of the int type now if we want to send and receive this over

want to send and receive this over

want to send and receive this over channels we also need to pass the

channels we also need to pass the

channels we also need to pass the channel so we’re gonna pass C for

channel so we’re gonna pass C for

channel so we’re gonna pass C for channel

channel

channel so it’s a Chan an int so see Chan int so

so it’s a Chan an int so see Chan int so

so it’s a Chan an int so see Chan int so then some value times five now if we

then some value times five now if we

then some value times five now if we want to pass that to the channel send

want to pass that to the channel send

want to pass that to the channel send that over this channel we can say see

that over this channel we can say see

that over this channel we can say see for the channel and then use the channel

for the channel and then use the channel

for the channel and then use the channel operator to send that over to the

operator to send that over to the

operator to send that over to the channels so basically what we’re doing

channels so basically what we’re doing

channels so basically what we’re doing is we’re sending to the channel some

is we’re sending to the channel some

is we’re sending to the channel some value times five because we’re sending

value times five because we’re sending

value times five because we’re sending it over the channel we actually don’t

it over the channel we actually don’t

it over the channel we actually don’t need to return anything in this function

need to return anything in this function

need to return anything in this function we just send it over the channel so

we just send it over the channel so

we just send it over the channel so we’ll come down here now and we’ll just

we’ll come down here now and we’ll just

we’ll come down here now and we’ll just say go foo the channel is boo vowel and

say go foo the channel is boo vowel and

say go foo the channel is boo vowel and the value let’s say is five and then

the value let’s say is five and then

the value let’s say is five and then let’s go ahead and do that again

let’s go ahead and do that again

let’s go ahead and do that again so let’s do three so now what we’ll do

so let’s do three so now what we’ll do

so let’s do three so now what we’ll do is we can receive those values the same

is we can receive those values the same

is we can receive those values the same way using the channel operator the

way using the channel operator the

way using the channel operator the channel operator like the arrow doesn’t

channel operator like the arrow doesn’t

channel operator like the arrow doesn’t ever point the other way so so the way

ever point the other way so so the way

ever point the other way so so the way that we can do that is you know if we

that we can do that is you know if we

that we can do that is you know if we can load a value or send a value to the

can load a value or send a value to the

can load a value or send a value to the channel or we can get a value from the

channel or we can get a value from the

channel or we can get a value from the channel and doing the same thing so we

channel and doing the same thing so we

channel and doing the same thing so we could say v1 colon equals whatever the

could say v1 colon equals whatever the

could say v1 colon equals whatever the receiving value from foo fowl is now

receiving value from foo fowl is now

receiving value from foo fowl is now this will just be the first value from

this will just be the first value from

this will just be the first value from foo vowel because we know we have two

foo vowel because we know we have two

foo vowel because we know we have two values we could hard-code it and say v2

values we could hard-code it and say v2

values we could hard-code it and say v2 is also whatever the the second foo of

is also whatever the the second foo of

is also whatever the the second foo of al is basically well let’s let’s go

al is basically well let’s let’s go

al is basically well let’s let’s go ahead and print this out too so now

ahead and print this out too so now

ahead and print this out too so now let’s do format dot print line v1 v2 so

let’s do format dot print line v1 v2 so

let’s do format dot print line v1 v2 so let’s go ahead and save that we’ll do go

let’s go ahead and save that we’ll do go

let’s go ahead and save that we’ll do go run go to go and sure enough we get 15

run go to go and sure enough we get 15

run go to go and sure enough we get 15 and 25 so so that’s just like a really

and 25 so so that’s just like a really

and 25 so so that’s just like a really basic example of using channels but

basic example of using channels but

basic example of using channels but obviously it gets a little more complex

obviously it gets a little more complex

obviously it gets a little more complex than that again just kind of like simple

than that again just kind of like simple

than that again just kind of like simple go routines we begin to have questions

go routines we begin to have questions

go routines we begin to have questions of how do we do synchronization how do

of how do we do synchronization how do

of how do we do synchronization how do we do things like for example we didn’t

we do things like for example we didn’t

we do things like for example we didn’t even actually synchronize these but then

even actually synchronize these but then

even actually synchronize these but then there’s also the concept of buffering

there’s also the concept of buffering

there’s also the concept of buffering with channels

with channels

with channels and and things like that and then also

and and things like that and then also

and and things like that and then also just iterating over channels like an

just iterating over channels like an

just iterating over channels like an aura case we could know how many

aura case we could know how many

aura case we could know how many sitemaps we’re gonna be working with so

sitemaps we’re gonna be working with so

sitemaps we’re gonna be working with so we could in theory know how many values

we could in theory know how many values

we could in theory know how many values were gonna return but obviously this is

were gonna return but obviously this is

were gonna return but obviously this is pretty sloppy the other thing you could

pretty sloppy the other thing you could

pretty sloppy the other thing you could do is you could say V 1 V 2 colon equals

do is you could say V 1 V 2 colon equals

do is you could say V 1 V 2 colon equals and then the return like this I think

and then the return like this I think

and then the return like this I think that trying to think I’m not sure you

that trying to think I’m not sure you

that trying to think I’m not sure you might do it that way I think it’s that

might do it that way I think it’s that

might do it that way I think it’s that way actually and let me comment these

way actually and let me comment these

way actually and let me comment these out real quick let’s see no maybe it’s

out real quick let’s see no maybe it’s

out real quick let’s see no maybe it’s worth a comma there is a way to do it

worth a comma there is a way to do it

worth a comma there is a way to do it that way either way though yeah ok that

that way either way though yeah ok that

that way either way though yeah ok that worked so but either way this would be

worked so but either way this would be

worked so but either way this would be hard-coded like let’s say you had in our

hard-coded like let’s say you had in our

hard-coded like let’s say you had in our case like yeah I think we have like 15

case like yeah I think we have like 15

case like yeah I think we have like 15 sitemaps or something but what if you

sitemaps or something but what if you

sitemaps or something but what if you had like a hundred should you as the

had like a hundred should you as the

had like a hundred should you as the programmer write all these out no you’re

programmer write all these out no you’re

programmer write all these out no you’re gonna want to iterate over the channels

gonna want to iterate over the channels

gonna want to iterate over the channels and when you iterate over them that’s

and when you iterate over them that’s

and when you iterate over them that’s when when the synchronization becomes an

when when the synchronization becomes an

when when the synchronization becomes an issue because actually right now you

issue because actually right now you

issue because actually right now you might have already noticed hey we’re

might have already noticed hey we’re

might have already noticed hey we’re using go routines and we didn’t have to

using go routines and we didn’t have to

using go routines and we didn’t have to wait for them right

wait for them right

wait for them right that’s because by default a channel the

that’s because by default a channel the

that’s because by default a channel the the send and receive part of the channel

the send and receive part of the channel

the send and receive part of the channel is gonna be blocking so as long as

is gonna be blocking so as long as

is gonna be blocking so as long as you’re hey you’re saying hey I want to

you’re hey you’re saying hey I want to

you’re hey you’re saying hey I want to receive those values it’s gonna block

receive those values it’s gonna block

receive those values it’s gonna block for you but again just like I’ve been

for you but again just like I’ve been

for you but again just like I’ve been saying in this entire series basic

saying in this entire series basic

saying in this entire series basic examples are really easy it’s when you

examples are really easy it’s when you

examples are really easy it’s when you actually go to use them in practice that

actually go to use them in practice that

actually go to use them in practice that you’re like wait that doesn’t work

you’re like wait that doesn’t work

you’re like wait that doesn’t work anymore okay so anyways that’s what

anymore okay so anyways that’s what

anymore okay so anyways that’s what we’re gonna learn about in the next

we’re gonna learn about in the next

we’re gonna learn about in the next tutorial is how we can actually more

tutorial is how we can actually more

tutorial is how we can actually more practically use channels but if you have

practically use channels but if you have

practically use channels but if you have any questions comments concerns or

any questions comments concerns or

any questions comments concerns or whatever up to this point feel free to

whatever up to this point feel free to

whatever up to this point feel free to lean below otherwise I will see you in

lean below otherwise I will see you in

lean 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 *