1
00:00:01,170 --> 00:00:07,650
All right, let's look at another important concept in the go programming language, and that is channels

2
00:00:07,920 --> 00:00:12,260
and channels are unique to go, or at least as far as I know, I've never seen them anywhere else.

3
00:00:12,690 --> 00:00:16,790
And channels are not that difficult, but they are incredibly useful.

4
00:00:16,830 --> 00:00:22,410
They are a means of sending information from one part of your program to another part of your program

5
00:00:22,650 --> 00:00:23,350
very easily.

6
00:00:23,880 --> 00:00:30,240
So we've so far, we've seen things we're going to have here is an empty package and our empty program.

7
00:00:30,240 --> 00:00:33,930
I have two packages of it saves me the last time I have Helper's, which is empty.

8
00:00:33,930 --> 00:00:37,740
It has nothing and I have my main package, which just has an empty main function.

9
00:00:38,220 --> 00:00:40,310
Now we've seen how to pass things around before.

10
00:00:40,320 --> 00:00:48,870
For example, I can say func print text and it takes a string as an argument and it returns nothing.

11
00:00:49,590 --> 00:00:55,980
And all it does is log the print line and then the string that I've passed it and then I can call that

12
00:00:55,980 --> 00:00:59,520
function from up here so I could say print text.

13
00:01:01,530 --> 00:01:01,980
Hi.

14
00:01:02,910 --> 00:01:08,910
So what I'm doing is passing information from my main function to my print text function, and that's

15
00:01:08,910 --> 00:01:10,210
one way of passing information.

16
00:01:10,710 --> 00:01:13,950
We also saw how you could do it using pointers and I'm not going to go through that again.

17
00:01:13,950 --> 00:01:15,930
You can just watch that lecture again if you wish to.

18
00:01:16,620 --> 00:01:20,730
Well, there's a third way of doing it, and that way is creating a channel.

19
00:01:20,850 --> 00:01:25,800
So I'm going to get rid of this function and I'm going to get rid of this call to that function.

20
00:01:26,670 --> 00:01:31,500
And we're back to an empty manuchehr or main function.

21
00:01:32,370 --> 00:01:37,140
So now I'm going to create a channel and you create channels in a way that's very similar to the way

22
00:01:37,140 --> 00:01:38,700
that we created maps.

23
00:01:38,700 --> 00:01:40,790
He used the make keyword.

24
00:01:41,130 --> 00:01:48,090
So I'll call this and into Champ, it's going to be a channel of its and it is created by saying make

25
00:01:49,050 --> 00:01:50,520
channel it.

26
00:01:51,270 --> 00:01:57,630
And all that says is I'm creating a channel, a place to send information which will be received in

27
00:01:57,630 --> 00:02:02,060
one or more places in my program, and that channel can only hold it.

28
00:02:02,100 --> 00:02:03,210
That's all that it can hold.

29
00:02:03,930 --> 00:02:05,610
So what do I do with that?

30
00:02:05,640 --> 00:02:06,920
Well, it's pretty straightforward.

31
00:02:06,930 --> 00:02:10,080
Let's create another function and I'll do it above the main function.

32
00:02:10,980 --> 00:02:17,850
And that's going to be called func calculate value.

33
00:02:18,960 --> 00:02:28,860
And it's going to take an argument of its channel and it's a type channel of it and it doesn't return

34
00:02:28,860 --> 00:02:29,220
anything.

35
00:02:30,630 --> 00:02:31,530
And what does this do?

36
00:02:31,530 --> 00:02:38,160
Well, in here I want to generate a random number and then I want to do something with that random number.

37
00:02:38,250 --> 00:02:42,970
And just for fun, I'm going to create my random number function over here and my helper function.

38
00:02:43,680 --> 00:02:47,640
So let's create a random number and it's not as easy as you might think.

39
00:02:48,750 --> 00:02:49,920
We can do it like this.

40
00:02:50,070 --> 00:02:58,800
We could say func random number that takes an argument and we want to create a random number from a

41
00:02:58,800 --> 00:03:03,690
pool of some size and that will be an entry and it will return the random number we calculate.

42
00:03:05,730 --> 00:03:07,930
So what do we do?

43
00:03:08,160 --> 00:03:10,890
We can say, first of all, we could say value.

44
00:03:14,390 --> 00:03:18,200
Is assigned and we're going to call a built in package.

45
00:03:18,230 --> 00:03:23,060
OK, we're going to call it built in package from the go language and it is called math.

46
00:03:23,840 --> 00:03:30,730
So we will use Rande and we're going to choose the correct package here.

47
00:03:30,740 --> 00:03:35,990
We want Rande and the function we want is IMT and there it is Matheran.

48
00:03:36,860 --> 00:03:38,900
And it's going to take an argument.

49
00:03:38,900 --> 00:03:41,970
And the argument it has to take is how big is the pool.

50
00:03:42,410 --> 00:03:45,030
So that will be m what we passed as a parameter.

51
00:03:45,620 --> 00:03:49,150
So now we've created around a number and we just want to return that.

52
00:03:49,160 --> 00:03:54,020
That's all that we want to do so we can say return value.

53
00:03:55,010 --> 00:03:57,170
OK, so apparently we've created a random number.

54
00:03:57,330 --> 00:04:03,260
Let's go back to our main program and let's get that random number in our calculate function.

55
00:04:04,400 --> 00:04:12,800
So random number, which is just a variable name, is assigned the value of and I want to call random

56
00:04:13,070 --> 00:04:16,130
number Helper's random number and I want to call it from some pool.

57
00:04:16,550 --> 00:04:23,210
Now, I could put 10 in here if I want to, but I'm going to use a constant const and I'll call it num

58
00:04:23,210 --> 00:04:23,660
pool

59
00:04:26,630 --> 00:04:32,660
and I'll make it equal to ten and then I'll just use that constant down here just because I can.

60
00:04:33,470 --> 00:04:38,130
So that would be no pool and we want to return that.

61
00:04:39,650 --> 00:04:40,550
Do we want to return it?

62
00:04:40,580 --> 00:04:43,450
No, we want to use our channel because what we've passed it is a channel.

63
00:04:44,180 --> 00:04:48,680
So I'm going to now pass that random number that I've just generated into my channel.

64
00:04:48,880 --> 00:04:54,620
And I do that using a very simple syntax int channel the channel name, and I'm going to pass it.

65
00:04:55,820 --> 00:04:56,480
Random number.

66
00:04:57,620 --> 00:05:01,610
OK, so now down here, I've created my main function.

67
00:05:01,610 --> 00:05:09,020
I've created that main channel and I want to close it once this check, once this program or this function

68
00:05:09,020 --> 00:05:10,340
is finished executing.

69
00:05:10,350 --> 00:05:11,630
Now we're in our main function.

70
00:05:11,640 --> 00:05:15,230
So technically I don't have to close it because the program will exit as soon as I'm done.

71
00:05:16,040 --> 00:05:19,100
But it's good to get in the habit of doing this.

72
00:05:19,100 --> 00:05:28,370
Defer clothes and my enchante, now that Dhafir is a word we haven't seen yet, and it's when you're

73
00:05:28,370 --> 00:05:32,300
going to be using regularly in our programming as we go through this course.

74
00:05:32,660 --> 00:05:40,190
And all first says is whatever comes after this keyword, defer, execute that as soon as the current

75
00:05:40,190 --> 00:05:41,450
function is done.

76
00:05:42,320 --> 00:05:48,470
Now that becomes useful because in situations where, for example, I open a file or I open a connection

77
00:05:48,470 --> 00:05:53,720
to the database, I don't want to leave those things open forever because every time you open a file

78
00:05:53,750 --> 00:06:00,440
on your file system, you're using one of a limited number of files that you can have open at the same

79
00:06:00,440 --> 00:06:00,790
time.

80
00:06:00,980 --> 00:06:06,260
And if you leave them all open every time you create a file or open a file for reading, for example,

81
00:06:06,860 --> 00:06:09,730
eventually you're going to run out of handles for the files.

82
00:06:10,220 --> 00:06:12,620
So you want to close them after you open them.

83
00:06:12,620 --> 00:06:14,420
And the same is true of database connections.

84
00:06:14,420 --> 00:06:19,070
You can have a limited number of database connections open at a given time, and when you run out,

85
00:06:19,070 --> 00:06:24,800
bad things happen and closing them after the function is finished, after you're finished using that

86
00:06:24,800 --> 00:06:26,410
function is just good practice.

87
00:06:26,420 --> 00:06:28,240
In fact, it's critical you have to do it.

88
00:06:28,490 --> 00:06:32,720
So here I don't want to have I can again, I can have a limited number of channels open.

89
00:06:32,720 --> 00:06:38,120
It's a big number, but if the program runs for seven months or 12 years or whatever, eventually you're

90
00:06:38,120 --> 00:06:38,920
going to run out of them.

91
00:06:39,410 --> 00:06:40,760
So I close them when I'm done with it.

92
00:06:41,060 --> 00:06:43,990
And here I'm just going to do that because it's good practice.

93
00:06:44,720 --> 00:06:51,590
Now we are going to call our calculate values, but I'm going to do it in a way that you haven't seen

94
00:06:51,590 --> 00:06:55,730
yet, and I'm going to do it by sending it off as a concurrent operation.

95
00:06:55,730 --> 00:06:59,690
I'm going to do it by running this routine in its own go routine.

96
00:06:59,870 --> 00:07:01,010
So it runs.

97
00:07:01,250 --> 00:07:04,970
And the nice thing about this is go routines run at the same time.

98
00:07:04,970 --> 00:07:09,350
So if I need to do a thousand things, I don't have to do them one at a time.

99
00:07:09,350 --> 00:07:12,890
I can fire off a thousand go routines, all of which run at the same time.

100
00:07:13,160 --> 00:07:13,970
Thousands, a big number.

101
00:07:13,970 --> 00:07:20,720
But you get the idea and you do that simply by saying go in front of the call to the function that you

102
00:07:20,720 --> 00:07:21,350
want to call.

103
00:07:21,920 --> 00:07:23,330
And that turns into a go routine.

104
00:07:23,330 --> 00:07:27,940
And I'm going to now call calculate value and I'm going to pass my channel into.

105
00:07:31,210 --> 00:07:37,790
There I have done that and now I need to listen for the response to that channel.

106
00:07:38,110 --> 00:07:44,800
So up here I passed Evalu into the channel down here, I want to get a value from the channel and I

107
00:07:44,800 --> 00:07:45,190
do that.

108
00:07:45,640 --> 00:07:47,200
It's a really straightforward syntax.

109
00:07:47,200 --> 00:07:54,670
I'm going to say my no, whatever it is, I'll just call it num is assigned the value of whatever comes

110
00:07:54,670 --> 00:07:55,720
from enchante.

111
00:07:58,430 --> 00:08:00,860
And then I'll print it out, log print online.

112
00:08:02,910 --> 00:08:06,250
And no, all right, so that should work.

113
00:08:06,630 --> 00:08:08,240
Now let's see what happens when we run this.

114
00:08:08,250 --> 00:08:14,040
I'm going to open my terminal window and I'm going to clear the screen and I'm going to just run the

115
00:08:14,040 --> 00:08:14,460
program.

116
00:08:14,470 --> 00:08:16,320
Go run, Mingo.

117
00:08:18,180 --> 00:08:20,460
I got one out of my pool of ten.

118
00:08:20,550 --> 00:08:21,400
Let's run it again.

119
00:08:21,420 --> 00:08:22,410
Go, run, go.

120
00:08:22,590 --> 00:08:23,210
I got one.

121
00:08:23,820 --> 00:08:24,810
Go, run, go.

122
00:08:25,380 --> 00:08:26,100
I got one.

123
00:08:27,390 --> 00:08:29,220
Which doesn't seem terribly random.

124
00:08:29,550 --> 00:08:30,750
And in fact, it's not.

125
00:08:31,140 --> 00:08:33,000
And it's not for a very simple reason.

126
00:08:33,000 --> 00:08:38,790
It would be random if this program was called would be pseudo random if the program wasn't exciting

127
00:08:38,790 --> 00:08:39,930
every time it was called.

128
00:08:40,770 --> 00:08:47,520
But in my helper's file, if I roll over inten, you actually see a little comment here sitting with

129
00:08:47,520 --> 00:08:49,950
the same value results in the same random sequence.

130
00:08:49,950 --> 00:08:54,060
Each run for different numbers, seed with the different values such as time.

131
00:08:54,420 --> 00:08:58,050
Now Unix and A. which yields a constantly changing number.

132
00:08:58,080 --> 00:08:58,520
Aha.

133
00:08:58,540 --> 00:09:01,170
That's all we have to do to get a random number here.

134
00:09:01,860 --> 00:09:06,060
So I need to see it and I see that by saying rande dot seed.

135
00:09:06,420 --> 00:09:10,290
And what I want to do is exactly what they told me inside of those parameters.

136
00:09:10,290 --> 00:09:18,270
I'll go with time dot now, which is the current time now, which is the current time, and then I want

137
00:09:18,270 --> 00:09:24,050
to get the Unix Nano and that gives me a different number every time I run this.

138
00:09:24,060 --> 00:09:30,930
So if we go back to our main program and we clear the screen and we run it again, this time I got two

139
00:09:30,930 --> 00:09:34,560
and I run it again and this time I got three and this time I get six.

140
00:09:34,560 --> 00:09:36,290
So now it is more random.

141
00:09:36,780 --> 00:09:41,520
So what it's returning is a random number between zero and my number.

142
00:09:41,520 --> 00:09:48,690
So if I make this number pool a thousand and clear the screen and run it again, this time I got 50,

143
00:09:48,990 --> 00:09:52,170
this time I got 412 730.

144
00:09:52,170 --> 00:09:56,040
But the important thing here is not the fact that we're getting random numbers, it's where we're getting

145
00:09:56,040 --> 00:09:56,970
that value from.

146
00:09:56,970 --> 00:09:57,890
So let's go through this.

147
00:09:58,620 --> 00:10:00,090
I have a main package.

148
00:10:00,300 --> 00:10:03,390
I have my main function that runs first.

149
00:10:03,960 --> 00:10:06,630
Actually, it declares this constant first, but this runs first.

150
00:10:07,230 --> 00:10:13,020
And then I create a channel, a channel that can only hold INTs if I try to pass a string or a float

151
00:10:13,020 --> 00:10:14,690
or a date or something else in there.

152
00:10:14,730 --> 00:10:15,540
It's not going to work.

153
00:10:15,800 --> 00:10:17,070
You can't even compile the program.

154
00:10:17,070 --> 00:10:18,080
It can only take it.

155
00:10:18,750 --> 00:10:23,700
Then I say when I'm finished running close this channel, which is good practice, not necessarily in

156
00:10:23,700 --> 00:10:25,680
the main function, but I'm going to do it anyway.

157
00:10:26,070 --> 00:10:31,500
And in later situations where we use this main function to initialize channels and then the main function

158
00:10:31,500 --> 00:10:35,130
continues to run for a long time, that's definitely critical.

159
00:10:35,130 --> 00:10:39,690
So do that get in a good habit of closing things that need to be closed and we'll be going through a

160
00:10:39,690 --> 00:10:41,070
number of things that need to be closed.

161
00:10:42,270 --> 00:10:48,540
So I create the channel, I defer the clothes, and then I say fire off a new go routine, run this

162
00:10:50,310 --> 00:10:54,030
by itself and its own routine and hand it the channel.

163
00:10:54,210 --> 00:10:54,600
Great.

164
00:10:54,600 --> 00:10:58,830
So I then this fires off up here.

165
00:10:59,340 --> 00:11:04,860
This as a go routine and that takes a channel is an argument and I generate a random number and then

166
00:11:04,860 --> 00:11:08,790
I just say, push that back into the channel you've just given me down here.

167
00:11:09,000 --> 00:11:16,380
I'm listening for that channel and when I get a value from it, it's passed into this variable num and

168
00:11:16,380 --> 00:11:17,220
then I just print it out.

169
00:11:17,610 --> 00:11:23,670
So that's just a really useful way of passing information from one package to another package or one

170
00:11:23,670 --> 00:11:26,250
part of your program to another part of your program.

171
00:11:26,430 --> 00:11:32,880
Incredibly powerful and very, very useful when you have more than one package in your program, which

172
00:11:32,880 --> 00:11:33,660
we're going to have.

173
00:11:34,080 --> 00:11:38,370
So those are channels and we'll be using them regularly as time goes on.
