1 00:00:00,380 --> 00:00:02,980 Here's an updated version of our longTask function 2 00:00:02,980 --> 00:00:05,940 that takes a random number of seconds to complete. 3 00:00:05,940 --> 00:00:09,590 We're using the Rand package to generate a random number. 4 00:00:09,590 --> 00:00:12,440 Again, that's not really important to understand in Go routines. 5 00:00:12,440 --> 00:00:15,670 But you can see the teacher's notes if you wanna learn more. 6 00:00:15,670 --> 00:00:20,580 We've also updated LongTask to return the number of seconds it took to its caller. 7 00:00:20,580 --> 00:00:21,810 So let's try running this here. 8 00:00:21,810 --> 00:00:28,790 It says starting long task and this time it slept for one second. 9 00:00:28,790 --> 00:00:34,050 Let's try running it again, and we can see this time it slept for 3 seconds. 10 00:00:34,050 --> 00:00:37,400 So it sleeps for a random number of seconds each time. 11 00:00:37,400 --> 00:00:41,370 You can see down here that we're taking the return value from long task, 12 00:00:41,370 --> 00:00:44,670 the number of seconds that it slept for, storing it in a variable and 13 00:00:44,670 --> 00:00:49,330 then printing that variable out to show the number of seconds that task took. 14 00:00:49,330 --> 00:00:52,300 Now suppose we wanna call longTask as a go routine. 15 00:00:52,300 --> 00:00:55,795 We can't just say time equals go longTask. 16 00:00:55,795 --> 00:00:58,360 We'll get a syntax error if we try to run this. 17 00:01:01,490 --> 00:01:04,320 Line 19, unexpected keyword go. 18 00:01:04,320 --> 00:01:06,340 We were expecting an expression. 19 00:01:07,650 --> 00:01:08,980 An even if that were allowed, 20 00:01:08,980 --> 00:01:12,360 what would we print in the line following the call to longTask. 21 00:01:12,360 --> 00:01:15,150 The go routine doesn't return the value right away. 22 00:01:15,150 --> 00:01:19,450 It's just not possible to communicate between go routines using simple return 23 00:01:19,450 --> 00:01:20,370 values. 24 00:01:20,370 --> 00:01:22,600 So instead, we're going to create a channel, and 25 00:01:22,600 --> 00:01:24,970 use that to communicate with our go routine. 26 00:01:24,970 --> 00:01:29,410 The channel will allow longTask to pass a value back to the main go routine. 27 00:01:29,410 --> 00:01:33,280 And when the main go routine encounters the instruction to read from the channel, 28 00:01:33,280 --> 00:01:36,309 it will wait until it has a value before it proceeds. 29 00:01:37,350 --> 00:01:40,280 We call the built-in make method to create a channel. 30 00:01:42,860 --> 00:01:46,840 We need to pass the type of value we are going to create to make. 31 00:01:46,840 --> 00:01:49,800 The type for a channel uses the abbreviation chan, 32 00:01:49,800 --> 00:01:54,150 and then we need to specify what type of values the channels will accept. 33 00:01:54,150 --> 00:01:57,070 We'll use a value of int in this case since we are going to 34 00:01:57,070 --> 00:02:00,610 be passing an integer with the number of seconds we waited. 35 00:02:00,610 --> 00:02:04,230 Then we'll store the new channel in a variable called channel. 36 00:02:06,130 --> 00:02:09,340 We're going to change the longTask functions declaration so 37 00:02:09,340 --> 00:02:13,550 that it accepts a channel as an argument, and get rid of the return value. 38 00:02:13,550 --> 00:02:18,804 So we'll pass our new channel as an argument to longTask, Get rid 39 00:02:18,804 --> 00:02:24,296 of the time variable and we'll cal longTask as a go routine. 40 00:02:24,296 --> 00:02:26,444 Then, instead of printing the time variable, 41 00:02:26,444 --> 00:02:28,610 we'll read a value from the channel. 42 00:02:28,610 --> 00:02:30,850 We do that using the arrow syntax. 43 00:02:33,020 --> 00:02:36,290 The arrow consists of a less than sign followed by a dash, and 44 00:02:36,290 --> 00:02:41,440 you can think of it as an arrow pointing from the channel into your code. 45 00:02:41,440 --> 00:02:46,090 Now we need to change the longTask methods declaration to match the code in main. 46 00:02:46,090 --> 00:02:51,220 We'll accept the parameter named channel of type chan int. 47 00:02:53,420 --> 00:02:53,940 That is, 48 00:02:53,940 --> 00:02:58,830 its type is channel, and the type of value that the channel holds are int values. 49 00:02:58,830 --> 00:03:01,280 And will get rid of the return value from longTask. 50 00:03:02,560 --> 00:03:06,429 At the end of the longTask function, instead of returning the amount of time 51 00:03:06,429 --> 00:03:09,520 we waited, we're going to write that value to the channel. 52 00:03:10,570 --> 00:03:12,450 So we'll take our channel parameter, and 53 00:03:12,450 --> 00:03:15,880 we'll use the arrow syntax to write a value to it. 54 00:03:15,880 --> 00:03:18,730 Think of this as the arrow pointing to the channel. 55 00:03:20,800 --> 00:03:24,498 That value will then be received and printed down in the main function. 56 00:03:24,498 --> 00:03:28,650 So let's save this and try running it. 57 00:03:29,700 --> 00:03:33,730 Starting long task, waiting for a random period. 58 00:03:33,730 --> 00:03:36,640 The amount of time we waited got written to the channel, and 59 00:03:36,640 --> 00:03:39,830 then it got printed out down in the main function. 60 00:03:39,830 --> 00:03:41,860 Let's try running it again. 61 00:03:41,860 --> 00:03:44,050 Starting with a long task. 62 00:03:44,050 --> 00:03:46,030 Waits for a random period. 63 00:03:46,030 --> 00:03:48,860 And it receives the amount of time it waited over the channel and 64 00:03:48,860 --> 00:03:51,150 prints it out down in the main function. 65 00:03:51,150 --> 00:03:55,890 Notice that each time the read from the channel down in the main function 66 00:03:55,890 --> 00:04:01,160 causes the main go routine to wait until the longTask go routine completes. 67 00:04:01,160 --> 00:04:03,650 We can start multiple go routines if we want. 68 00:04:03,650 --> 00:04:05,790 And they can all write to the same channel. 69 00:04:05,790 --> 00:04:07,750 So let's setup a loop here. 70 00:04:07,750 --> 00:04:14,350 We'll say, for i =, we'll start it a 1 while i is less than or 71 00:04:14,350 --> 00:04:19,790 equal to three increment i after each pass through the loop. 72 00:04:22,888 --> 00:04:27,320 And then here within the loop we'll call longTask as a go routine. 73 00:04:29,380 --> 00:04:32,370 So this will wind up calling longTask three times. 74 00:04:32,370 --> 00:04:33,870 Then we'll create another for loop. 75 00:04:36,800 --> 00:04:39,230 That uses the same parameters. 76 00:04:39,230 --> 00:04:44,570 It'll start at one while it's less than or equal to three we'll increment it by one. 77 00:04:44,570 --> 00:04:48,330 And within that loop we'll read from the channel 78 00:04:49,950 --> 00:04:51,920 the number of seconds that our go routine took. 79 00:04:53,770 --> 00:04:55,160 Let's save this and try running it. 80 00:04:56,780 --> 00:05:00,730 We see that our three go routines start almost simultaneously. 81 00:05:00,730 --> 00:05:03,470 Print starting long task three times. 82 00:05:03,470 --> 00:05:07,068 Two of the Go routines took 3 seconds to complete, and 83 00:05:07,068 --> 00:05:10,270 the third took 4 seconds to complete. 84 00:05:10,270 --> 00:05:14,520 Because we read from the channel the same number of times that we wrote to it, 85 00:05:14,520 --> 00:05:18,740 the program waits until all the Go routines have finished before it exits. 86 00:05:18,740 --> 00:05:22,750 This is just been a quick taste of Go's concurrency features. 87 00:05:22,750 --> 00:05:26,780 Go routines and channels are an easy way to maximize your program's efficiency.