Press "Enter" to skip to content

Channels – Go Lang Practical Programming Tutorial p.22


what is going on everybody and welcome
to part 22 of the Golan tutorial series
in this tutorial what we’re talking
about is channels so the idea of
channels with go is to use them with
your go routines in order to send and
receive values between them using the
channel operator and the channel
operator is just your less than sign and
then a minus sign basically it looks
like an arrow so let’s go ahead and just
start with a really I don’t know what I
was about to type but anyway package
main will import format will also spell
import right and then won’t have func
main and and what we’ll do first is we
should let’s make a channel so generally
to make a channel you’re gonna have make
and then you’ll have the the channel so
we’re gonna make a Chan and then you
give the room the type of the channel so
in this case it’s going to be type int
so we can assign this and give this a
you know to a vat variable by doing foo
Val
so foo val is a a channel of the int
type now of course just like a lot of
things in go if you wanted that channel
it could contain a type that you created
with a struct so if you wanted to pass
be able to pass you know every time an
integer a string and then a float or
something like that you could create a
custom type and pass that instead so
anyways our channel will be foo fowl and
then we’re going to do is we’re gonna
come up here and we’re gonna create func
foo and basically what we’re gonna do
with foo is let’s just say we’re going
to take a value so we’re gonna take you
know some value and then we want to
multiply it by 5 so this foo function
needs to at least accept in some value
and that’ll be of the int type now if we
want to send and receive this over
channels we also need to pass the
channel so we’re gonna pass C for
channel
so it’s a Chan an int so see Chan int so
then some value times five now if we
want to pass that to the channel send
that over this channel we can say see
for the channel and then use the channel
operator to send that over to the
channels so basically what we’re doing
is we’re sending to the channel some
value times five because we’re sending
it over the channel we actually don’t
need to return anything in this function
we just send it over the channel so
we’ll come down here now and we’ll just
say go foo the channel is boo vowel and
the value let’s say is five and then
let’s go ahead and do that again
so let’s do three so now what we’ll do
is we can receive those values the same
way using the channel operator the
channel operator like the arrow doesn’t
ever point the other way so so the way
that we can do that is you know if we
can load a value or send a value to the
channel or we can get a value from the
channel and doing the same thing so we
could say v1 colon equals whatever the
receiving value from foo fowl is now
this will just be the first value from
foo vowel because we know we have two
values we could hard-code it and say v2
is also whatever the the second foo of
al is basically well let’s let’s go
ahead and print this out too so now
let’s do format dot print line v1 v2 so
let’s go ahead and save that we’ll do go
run go to go and sure enough we get 15
and 25 so so that’s just like a really
basic example of using channels but
obviously it gets a little more complex
than that again just kind of like simple
go routines we begin to have questions
of how do we do synchronization how do
we do things like for example we didn’t
even actually synchronize these but then
there’s also the concept of buffering
with channels
and and things like that and then also
just iterating over channels like an
aura case we could know how many
sitemaps we’re gonna be working with so
we could in theory know how many values
were gonna return but obviously this is
pretty sloppy the other thing you could
do is you could say V 1 V 2 colon equals
and then the return like this I think
that trying to think I’m not sure you
might do it that way I think it’s that
way actually and let me comment these
out real quick let’s see no maybe it’s
worth a comma there is a way to do it
that way either way though yeah ok that
worked so but either way this would be
hard-coded like let’s say you had in our
case like yeah I think we have like 15
sitemaps or something but what if you
had like a hundred should you as the
programmer write all these out no you’re
gonna want to iterate over the channels
and when you iterate over them that’s
when when the synchronization becomes an
issue because actually right now you
might have already noticed hey we’re
using go routines and we didn’t have to
wait for them right
that’s because by default a channel the
the send and receive part of the channel
is gonna be blocking so as long as
you’re hey you’re saying hey I want to
receive those values it’s gonna block
for you but again just like I’ve been
saying in this entire series basic
examples are really easy it’s when you
actually go to use them in practice that
you’re like wait that doesn’t work
anymore okay so anyways that’s what
we’re gonna learn about in the next
tutorial is how we can actually more
practically use channels but if you have
any questions comments concerns or
whatever up to this point feel free to
lean below otherwise I will see you in
the next tutorial
Please follow and like us:

Be First to Comment

Leave a Reply