1 00:00:00,100 --> 00:00:04,800 In this first section, I'm going to go over some of the syntax of go, which you may 2 00:00:04,800 --> 00:00:08,900 already be familiar with, but there are various little bits of syntax that you may not have used and I 3 00:00:08,900 --> 00:00:12,800 want to recap those and also look into some of the areas like 4 00:00:12,800 --> 00:00:16,700 methods and interfaces where there's some depth as worth recapping or 5 00:00:16,700 --> 00:00:20,900 going into as you may not have seen it before. So, to start with, I want 6 00:00:20,900 --> 00:00:24,800 to look at something very familiar, which is the for Loop and in particular the use of 7 00:00:24,800 --> 00:00:28,400 break and continue and at the same time, what happens with a switch statement? 8 00:00:29,000 --> 00:00:30,100 So let's get 9 00:00:30,100 --> 00:00:34,600 We have our world, we don't need that program anymore although we use it just to check that the environment is working. 10 00:00:34,900 --> 00:00:38,900 So let's just use, go run and yeah. Okay, let's looks like it's 11 00:00:38,900 --> 00:00:42,800 good. So the for Loop so the for-loop syntax you are familiar 12 00:00:42,800 --> 00:00:46,900 with because you've been using go for a while, I hope. And so the basic one is just 13 00:00:46,900 --> 00:00:50,800 a simple Loop like this. And what I'm going to do is look at the use 14 00:00:50,800 --> 00:00:54,900 of break and continue, so break obviously gets you 15 00:00:54,900 --> 00:00:58,800 out of a loop so you can use that keyword like this and it will drop 16 00:00:58,800 --> 00:01:00,000 you out of the loop. 17 00:01:00,100 --> 00:01:04,900 Lee and continue will take you back to the beginning of the loop. So it's essentially a 18 00:01:04,900 --> 00:01:08,600 go-to to the beginning of the loop. Both of these can also be used with 19 00:01:08,600 --> 00:01:12,900 labels in particular break. So if we wanted to break out of a particular Loop, when 20 00:01:12,900 --> 00:01:16,800 things were nested, we could actually name the loop. So we will say, you know, 21 00:01:17,200 --> 00:01:21,900 my label and then I could say break my label, 22 00:01:21,900 --> 00:01:25,700 which will take me out of that particular Loop here. So 23 00:01:25,800 --> 00:01:29,900 this is a little example. Let's make ourselves something a bit more interesting. 24 00:01:30,300 --> 00:01:34,900 And I'm going to make a program that counts at numbers and accounts, even and odd 25 00:01:34,900 --> 00:01:38,500 numbers. So, let's go like this, going to have some 26 00:01:38,500 --> 00:01:41,800 variables to count the number of even and odd numbers in my program. 27 00:01:42,700 --> 00:01:46,600 And I'm going to give myself a list of numbers. So I'm going to like this. 28 00:01:47,700 --> 00:01:51,100 So just create a list. This is make something very silly. 29 00:01:52,500 --> 00:01:56,700 Like that. And I'm going to range over those numbers. So we're going to say 30 00:02:01,000 --> 00:02:05,500 numbers. Okay, there we go. Is range over those numbers. Notice, I use the 31 00:02:05,500 --> 00:02:09,800 underscore here because I don't actually care about the index in here. I just care about the number n 32 00:02:09,800 --> 00:02:13,600 itself, and I'm going to go through an account. So I'm going to say, 33 00:02:14,000 --> 00:02:18,400 if n %, 2 equals 0, I is even 34 00:02:19,700 --> 00:02:23,700 then do that. And so, we'll just do even less equals 1 35 00:02:23,900 --> 00:02:27,500 or plus 1. And at the end is to a Quick Print 36 00:02:27,500 --> 00:02:30,500 half. Even how many of those are we got 37 00:02:30,700 --> 00:02:32,000 Order, how many of those we got? 38 00:02:33,800 --> 00:02:34,400 You know what? 39 00:02:36,400 --> 00:02:40,900 This is go run out. There you go. So, there are three even numbers in there, 40 00:02:40,900 --> 00:02:44,800 and there are five odds. So you can see the odds are in here and the 41 00:02:44,800 --> 00:02:48,800 even CSO the evens or to 8 and 10. All right. So that's a very simple 42 00:02:48,800 --> 00:02:52,700 program with a fairly simple Loop. Now, what we could do is we may 43 00:02:52,700 --> 00:02:56,700 decide there are some numbers we want to exclude. For example, let's 44 00:02:56,700 --> 00:03:00,900 not count 0 as even or odd that these only 45 00:03:00,900 --> 00:03:04,800 to be an order. Let's just shove it in somewhere else. Now, let's just say, we decided 46 00:03:04,800 --> 00:03:06,100 that something else that needs to be. 47 00:03:06,300 --> 00:03:10,900 10 different way and we're going to excluded all together and counted as if it is if it 48 00:03:10,900 --> 00:03:14,900 wasn't a number. So I'm going to have a total number of numbers 49 00:03:15,500 --> 00:03:16,900 and I'm going to put myself a little 50 00:03:18,800 --> 00:03:22,100 If and equals zero continue. 51 00:03:23,800 --> 00:03:27,300 So it's going to go like that and total, 52 00:03:28,000 --> 00:03:32,200 we're going to add up the total number of numbers and I just adjust this. So we get a total here. 53 00:03:35,400 --> 00:03:39,600 So this continue here is just going to say, if I've got zero, I'm not going to count it as even or 54 00:03:39,600 --> 00:03:43,800 odd, just run that. There we go. So there's nine numbers in total, and you can see, 55 00:03:44,100 --> 00:03:48,900 five and three, even at odds. So you obviously skipped the 0. Obviously, you could do this a different 56 00:03:48,900 --> 00:03:52,900 way. You could come in here and you can say, you know, what do this? It's 57 00:03:52,900 --> 00:03:53,500 just indent 58 00:03:53,700 --> 00:03:57,900 At or you could. So you could count like this, that would 59 00:03:57,900 --> 00:04:01,800 give you the same result. But continue There is a 60 00:04:01,800 --> 00:04:05,900 handy way especially if you've got a lot of logic, you want to deal with. Now, another thing we could 61 00:04:05,900 --> 00:04:09,900 do is let's suppose that finding zero was a sort of marker in the 62 00:04:09,900 --> 00:04:13,800 list that said you had to stop. Then you could do something like this. 63 00:04:14,500 --> 00:04:15,900 It's clean this up a bit. 64 00:04:17,900 --> 00:04:21,800 So that's going to count even and odd numbers until it hits zero and it's going to break out of that 65 00:04:21,800 --> 00:04:25,800 Loop. So, there we go. There's only one even number 4, 0, that's 66 00:04:25,800 --> 00:04:29,900 number two right here. And another you can do is you 67 00:04:29,900 --> 00:04:33,900 can label this Loop so you could break out of it. So for example, 68 00:04:33,900 --> 00:04:37,900 we could do something very silly like go around this Loop, some number of times, 69 00:04:38,600 --> 00:04:41,800 let's just do a classic thing. It's going to go around, this Loop ten times. 70 00:04:43,900 --> 00:04:44,800 Let's do this. 71 00:04:47,200 --> 00:04:51,600 And we can specifically decide to give up when we find a zero. 72 00:04:51,900 --> 00:04:55,800 So I'm going to say here, abort, its course called this 73 00:04:55,800 --> 00:04:59,200 abort and if we find a zero then we're going to give up 74 00:04:59,200 --> 00:05:03,800 immediately. So let's just run this guy so that gave up immediately 75 00:05:03,800 --> 00:05:07,900 because it broke out of this Loop. If there's no label. Now, let's just run it like that. 76 00:05:09,100 --> 00:05:13,900 Sorry good old go tells me, I miss something. It goes around 10 77 00:05:13,900 --> 00:05:16,800 times. So that aboard label allowed 78 00:05:16,900 --> 00:05:20,800 To jump right out of the loop there and jump out 79 00:05:20,800 --> 00:05:24,700 of two levels of looping. So the commute of you have nested Loop in. You want 80 00:05:24,700 --> 00:05:27,900 to get out altogether, it's just try. 81 00:05:29,100 --> 00:05:29,900 One last thing. 82 00:05:35,700 --> 00:05:39,900 And that you see what that did was it continued up to here. So that actually jumped out of the loop and up 83 00:05:39,900 --> 00:05:43,900 to the loop above it. So with labels, break an abort 84 00:05:43,900 --> 00:05:47,800 allows you to jump around within Loops. There are close to being a go-to and can be 85 00:05:47,900 --> 00:05:51,800 kind of useful, especially if you have some sort of condition where you want to do something. 86 00:05:52,300 --> 00:05:56,800 For example, one common thing is, if you have, like, I'd say a debug mode, where if a certain thing 87 00:05:56,800 --> 00:06:00,700 happens, you need to get out and terminate, you could use a break for that or maybe you 88 00:06:00,700 --> 00:06:04,700 have some mode where you wanted to do is, let's say counting of events, but not 89 00:06:04,700 --> 00:06:05,300 actually, 90 00:06:05,400 --> 00:06:09,900 You the events then you could use a continue to jump around in a loop useful. If you want to sort 91 00:06:09,900 --> 00:06:13,700 of jump quickly in general, if you're using a lot of them, I think it's bad 92 00:06:13,700 --> 00:06:17,700 sign because your Loop is going to be hard to understand. But at the beginning of the loop, 93 00:06:17,700 --> 00:06:21,800 where you want to add a board or jump again, go around 94 00:06:21,800 --> 00:06:25,300 around the loop. Again, without doing work, both break and continue can be super useful. 95 00:06:25,300 --> 00:06:29,900 Another place that break can be useful. As in a switch statement, especially 96 00:06:29,900 --> 00:06:33,700 if the switch statement is rather complicated and so, you can get 97 00:06:33,700 --> 00:06:35,400 out of the switch and you can even 98 00:06:35,400 --> 00:06:39,900 Even get out of a loop inside a switch by using break with a label. I'm going to 99 00:06:39,900 --> 00:06:43,900 demonstrate that by modifying the program that was used in the for Section 100 00:06:43,900 --> 00:06:47,900 which is counting even and odd numbers to have a special case 101 00:06:47,900 --> 00:06:51,700 where it comes across a negative number and aborts. So, first of all, let's have a look at 102 00:06:51,700 --> 00:06:55,800 that original thing. I've cleaned it up just slightly as you can see. 103 00:06:55,800 --> 00:06:59,700 It's got a few different things going on in here. And this, when you've got a lot of 104 00:06:59,700 --> 00:07:03,900 ifs like this, it can actually be really useful to stop and say, hey a switch statement 105 00:07:03,900 --> 00:07:05,300 is useful here rather than having 106 00:07:05,400 --> 00:07:09,500 At long nested thing. So let's turn this into a switch and then 107 00:07:09,500 --> 00:07:13,700 we'll we'll work from there. So switch statement, syntax is like this. So we got these 108 00:07:13,700 --> 00:07:17,900 three cases, right? We've got this case where it's zero. We've got a 109 00:07:17,900 --> 00:07:20,300 case where it's even 110 00:07:22,700 --> 00:07:26,600 We've got the other case I'm going to use default for that. And so, let's clean 111 00:07:26,600 --> 00:07:30,900 this stuff up here. So okay, so they've got a 3. So if we come across is 112 00:07:31,700 --> 00:07:35,600 0, we come across an even number or we come across something else so we can just change that. So 113 00:07:36,100 --> 00:07:40,900 in this case we're going to just add one to even if it's not 0 or that when they start there's and 114 00:07:40,900 --> 00:07:44,900 I'm going to count zeros this time like this. So what do that? And 115 00:07:44,900 --> 00:07:46,300 let's just add this into here. 116 00:07:48,400 --> 00:07:51,100 And down the bottom here, let's put in the zero account. 117 00:07:56,500 --> 00:08:00,900 Okay, so this switch statement is actually much easier to read than those ifs that we had before, especially when you 118 00:08:00,900 --> 00:08:04,800 get a long list of if, sometimes it's really worth going. Hey switch statement makes a lot of sense. 119 00:08:05,300 --> 00:08:09,700 It's especially hard to think about switch. If you're coming from see where you had been 120 00:08:09,700 --> 00:08:13,900 switching on a single thing, whereas here, you can have it as we have three 121 00:08:13,900 --> 00:08:17,900 completely different cases in here and they could be, much, more complicated. All these are 122 00:08:17,900 --> 00:08:21,800 using n, but another case could use completely different variables or 123 00:08:21,800 --> 00:08:25,700 completely different conditions. So let's see if I haven't forgotten anything go. 124 00:08:26,400 --> 00:08:30,700 All right, there we go. So now we got olds even zeros and that so great. Now 125 00:08:30,700 --> 00:08:34,700 suppose that we wanted to terminate this switch a 126 00:08:34,700 --> 00:08:38,200 bit early. If you notice in here 127 00:08:38,600 --> 00:08:42,300 we could say okay well I'm done here. I want to get out of this 128 00:08:42,800 --> 00:08:46,900 here. Now we might we don't need to do that like we would in see there's no fall 129 00:08:46,900 --> 00:08:50,300 through happening here because that happened on the 130 00:08:50,300 --> 00:08:54,800 first the first time that case hit. But we may 131 00:08:54,800 --> 00:08:56,200 want to if there was some other 132 00:08:56,300 --> 00:09:00,800 Ian actually terminate completely so break can be used. 133 00:09:00,800 --> 00:09:04,900 So for example you there could be some sort of if in here and you could say okay well get out 134 00:09:04,900 --> 00:09:08,000 of here if that thing happens otherwise do some other stuff. 135 00:09:09,100 --> 00:09:13,700 The other thing we can do is we can give up completely if there's a problem. So for example, I'm going to use 136 00:09:13,700 --> 00:09:17,900 break here to get out of this switch and to get out of the loop. So I'm going to go back happening 137 00:09:17,900 --> 00:09:21,700 that keyword and bought in here. So here's a label abort. And I'm going to 138 00:09:21,700 --> 00:09:25,600 change this slightly so that it recognizes odd 139 00:09:25,600 --> 00:09:26,100 numbers. 140 00:09:26,300 --> 00:09:30,900 Chloe. So, I'm going to say that Isis counting zeroes evens, 141 00:09:30,900 --> 00:09:34,700 and odds. And then if it comes across something else, that's not one of 142 00:09:34,700 --> 00:09:38,900 those two things, then we, then we can abort. Now, let's 143 00:09:38,900 --> 00:09:42,300 go into here and we're going to go break a board like that. 144 00:09:42,800 --> 00:09:46,900 Okay, so what happens in this Loop now is if there's a negative number none of 145 00:09:46,900 --> 00:09:50,900 the previous three cases, apply, and the default actually happens. You 146 00:09:50,900 --> 00:09:54,700 might be wondering about this case here, for when the number is 147 00:09:54,800 --> 00:09:56,100 negative, or the case of 148 00:09:56,300 --> 00:10:00,700 Above. But, in fact, when you do module mod a negative, number you, still get Negative something. 149 00:10:00,700 --> 00:10:04,400 So neither of those cases will apply in the case where it's negative, and you'll hit the 150 00:10:04,400 --> 00:10:08,700 default and we will actually break from the loop. And if you take a quick look up here, 151 00:10:09,000 --> 00:10:13,800 go run is only counted to even numbers. It hasn't counted the number 10, 152 00:10:13,800 --> 00:10:17,800 which is after the negative number, because it broke at that point, but we can make a bit more 153 00:10:17,800 --> 00:10:21,900 explicit. And this is where you might actually use this kind of thing. Let's make ourselves 154 00:10:21,900 --> 00:10:22,500 an error. 155 00:10:23,300 --> 00:10:27,800 So we would have this error here and in the case that there's a negative number will actually 156 00:10:27,800 --> 00:10:30,400 set that error. And I'm going to use the errors package. 157 00:10:32,700 --> 00:10:35,300 I want to import that and I'm going to say New Era. 158 00:10:36,800 --> 00:10:39,200 Found negative number. 159 00:10:40,800 --> 00:10:44,500 As always, though set. And then at the bottom here, we can actually say, you know, if there was an error. 160 00:10:48,000 --> 00:10:49,100 Just print it out. 161 00:10:52,700 --> 00:10:56,100 Let's print the error like that, so do that and let's just run it 162 00:10:57,200 --> 00:11:01,900 error found a negative number and it broke from the loop at that point. So the break statement within 163 00:11:01,900 --> 00:11:05,900 a switch here, can jump out of the switch and out of the loop using a label, which is 164 00:11:05,900 --> 00:11:09,900 Handy. And of course, it's nothing like in C where you have to break every case within the 165 00:11:09,900 --> 00:11:13,700 switch because otherwise it will fall through. You can also do continue. 166 00:11:14,000 --> 00:11:18,800 Another thing we can do here is we can modify this slightly so that if it sees an odd, number it just carries on but 167 00:11:18,800 --> 00:11:22,400 doesn't count it. So, sorry, a negative number. So let's just do 168 00:11:22,600 --> 00:11:26,200 This is moved a total down to here. So only counts if it gets to here 169 00:11:27,100 --> 00:11:30,600 and every time we get a 170 00:11:31,500 --> 00:11:34,100 negative we don't need this anymore. 171 00:11:36,300 --> 00:11:40,900 We don't need this and let's kill this. So this 172 00:11:40,900 --> 00:11:44,800 has been clean up a little bit every time it gets a negative number is going to continue to that aboard 173 00:11:44,800 --> 00:11:48,700 label up play and perhaps we should say let's change that to ignore 174 00:11:48,700 --> 00:11:51,300 negatives and support doesn't seem like a very good name anymore. 175 00:11:55,500 --> 00:11:59,700 That's from them. So now you can see the III even 176 00:11:59,700 --> 00:12:03,800 numbers because it did actually count the 10 at the end and what it 177 00:12:03,800 --> 00:12:07,900 did was when it hit that negative, it jumped across and you can also see that there's a 178 00:12:07,900 --> 00:12:11,700 total of nine things counted. If we were just to add in 179 00:12:11,700 --> 00:12:15,300 here then numbers just so we can see. 180 00:12:17,300 --> 00:12:21,700 How many there were you can see There Were Ten in total so we ignored one. 181 00:12:21,900 --> 00:12:25,900 So that's really continue and break within the context of a switch 182 00:12:25,900 --> 00:12:28,500 and a for Loop both useful in some circumstances. 183 00:12:33,600 --> 00:12:37,600 Another thing that's worth reviewing, after you've been using go for a little while, is the codon 184 00:12:37,600 --> 00:12:41,400 equals to declare a variable. And remember, that is a declaration 185 00:12:41,700 --> 00:12:45,700 and has interactions with if and scoping that sometimes can 186 00:12:45,700 --> 00:12:49,800 trip you up, if you're not thinking about it too carefully. And I know that personally, I've been 187 00:12:49,800 --> 00:12:53,800 tripped up a couple of times, so let's just look at declaring a variable. So I'm going to do a 188 00:12:53,800 --> 00:12:57,900 little simple example. I'm going to create a couple of slices and going to do two 189 00:12:57,900 --> 00:13:01,700 different ones. I'm going to say in slices, going to be just some 190 00:13:01,800 --> 00:13:02,800 list of numbers. 191 00:13:03,000 --> 00:13:03,400 so, 192 00:13:06,700 --> 00:13:10,900 So there we go. Just a list of numbers. And I'm also going to have a string slice, which is going to have some 193 00:13:10,900 --> 00:13:14,700 strings in it. So I'm going to declare it like this and that's going to 194 00:13:14,700 --> 00:13:18,400 be sky, was 195 00:13:19,300 --> 00:13:22,800 the color kind of like that 196 00:13:23,400 --> 00:13:27,700 and I'm going to use some work with those. Let's just do a 197 00:13:27,700 --> 00:13:30,800 declaration. So it really classic. Kind of Declaration will be something like this 198 00:13:32,000 --> 00:13:36,000 and we will look at that in slice and we would say you know if it's got 199 00:13:36,500 --> 00:13:40,900 Not empty. Then we're going to do something otherwise, when do something else. So let's just go like this, 200 00:13:41,200 --> 00:13:45,500 it might go in here. We like to say in slice has 201 00:13:46,700 --> 00:13:50,900 an element's yourself, rather simple and do that. 202 00:13:50,900 --> 00:13:54,200 And then down here we could do you know 203 00:13:55,300 --> 00:13:59,300 In slice has no elements 204 00:13:59,300 --> 00:14:03,800 like that and we do exactly the same thing for 205 00:14:03,800 --> 00:14:06,000 the string, Slice on the copy, paste on that. 206 00:14:08,400 --> 00:14:09,600 Into string. 207 00:14:12,200 --> 00:14:16,800 Okay, so there we go. It's going to look at these two slices and tell us some information about 208 00:14:16,800 --> 00:14:20,800 them and if I were to close my strings, it would be even better. 209 00:14:21,400 --> 00:14:24,400 Okay. So there's pretty simple program does. Let's run it. 210 00:14:25,700 --> 00:14:29,200 And whoops. What have I forgotten to do here? 211 00:14:30,800 --> 00:14:34,500 That's right. The city on the internet. Let's just do that. Okay. 212 00:14:34,900 --> 00:14:38,700 So this in slices, 8 elements, as we might rather than those of five 213 00:14:38,700 --> 00:14:42,600 elements and you can see the N here was declared 214 00:14:42,600 --> 00:14:46,500 here and it was re declared here and there's no error 215 00:14:46,500 --> 00:14:50,800 occurring. Notice that if we had done this, let me just take this decoration 216 00:14:50,800 --> 00:14:51,500 out of here. 217 00:14:54,700 --> 00:14:55,700 Let's put it here. 218 00:14:57,300 --> 00:14:58,400 Now, do the same thing here. 219 00:15:04,500 --> 00:15:08,700 Just run that. Oh, no new variables, I'd line 18. So, right here 220 00:15:08,700 --> 00:15:12,300 saying this, has been declared before because these two are within the same 221 00:15:12,300 --> 00:15:16,900 scope, but when it was inside the, if here, it was actually not in 222 00:15:16,900 --> 00:15:19,400 the same scope, the scope of that n 223 00:15:20,400 --> 00:15:24,800 is this if statement is whole, if statement up all the way 224 00:15:24,800 --> 00:15:28,800 through, including the else is quite tempting. If you've been using C or 225 00:15:28,800 --> 00:15:32,900 something like that, to think it might just be the first block here, but in fact, 226 00:15:32,900 --> 00:15:34,100 it's being declared in. 227 00:15:34,200 --> 00:15:38,700 At the scope of this statement. And so the same thing, let's just do the same thing 228 00:15:38,700 --> 00:15:40,700 here. This is put this back 229 00:15:44,000 --> 00:15:48,900 So when you have those two things, then you get no error because they're 230 00:15:48,900 --> 00:15:52,900 completely different scope. The other thing, the surprising sometimes is that n 231 00:15:52,900 --> 00:15:56,800 is valid here. So let's just for the fun of it, stick it 232 00:15:56,800 --> 00:15:58,100 in here like this. 233 00:16:01,800 --> 00:16:05,900 Let's run that and you're going to see that well because it does have some elements. 234 00:16:06,000 --> 00:16:10,500 So let's just say it's just messed. This up, just break 235 00:16:10,500 --> 00:16:14,900 this and you can see, has no elements and actually is lying 236 00:16:14,900 --> 00:16:18,900 because I messed up. The FM says eight, but that variable was valid inside that 237 00:16:18,900 --> 00:16:22,700 entire scope and of course, this can be quite deep and nested. So 238 00:16:22,700 --> 00:16:26,900 watch out for that when you're doing declarations these two things are, these 239 00:16:26,900 --> 00:16:28,000 are different variables. 240 00:16:28,800 --> 00:16:32,500 So you'll know if you've been using go a little bit that sometimes you 241 00:16:32,500 --> 00:16:36,800 do do re declarations in the same scope and I'm going to show you an 242 00:16:36,800 --> 00:16:40,100 example of that. Let's just simplify this a bit. 243 00:16:40,900 --> 00:16:44,300 Let's go like this. Now if you remember that in the 244 00:16:44,800 --> 00:16:48,800 wonderful world of printf, you can actually get an error. Return printf will 245 00:16:48,800 --> 00:16:52,700 actually return to you the number of characters and an error. So you can do 246 00:16:52,700 --> 00:16:56,400 something like this. And so I'm going to say printf in 247 00:16:56,400 --> 00:16:58,000 slice has sent the 248 00:17:02,800 --> 00:17:06,900 So there it is. And I'm going to do the following. I'm going 249 00:17:06,900 --> 00:17:10,900 to do this. I'm going to do exactly the same thing again, but I'm gonna do 250 00:17:10,900 --> 00:17:14,900 it for string slice and this is going to give us an error. When I 251 00:17:14,900 --> 00:17:17,600 run, this is going to complain. So, let's just do the following 252 00:17:19,800 --> 00:17:23,900 Okay, so it says no new variables are line. 12 line, 12 is here because n and error will 253 00:17:23,900 --> 00:17:27,800 both declared above. Okay, let's just change this slightly and 254 00:17:27,800 --> 00:17:31,900 do that and now I'm going to actually print those out, just so we see 255 00:17:31,900 --> 00:17:33,900 them and also, they go doesn't complain. 256 00:17:39,000 --> 00:17:41,100 And let's print out the error too. 257 00:17:44,300 --> 00:17:48,900 So I hope they'll be none. Okay, so now 258 00:17:49,700 --> 00:17:53,700 you'll notice that it didn't complain about error being re declared and it 259 00:17:53,700 --> 00:17:57,800 was apparently being re declared as a convenience, what go allows you 260 00:17:57,800 --> 00:18:01,400 to do is if you're doing a declaration like this where there are multiple 261 00:18:01,400 --> 00:18:05,800 variables and some of them have been declared before, you're allowed to reuse, them 262 00:18:05,800 --> 00:18:09,500 is the same same value. So this declaration here and one 263 00:18:09,500 --> 00:18:13,900 error is actually only declaring N1 is not actually read. 264 00:18:14,100 --> 00:18:18,800 Glaring error, it's reusing it and you just be careful if it was a different type, of course, you could get some 265 00:18:18,800 --> 00:18:22,700 sort of error. So that's a convenience that you'll see throughout and in fact, because 266 00:18:22,700 --> 00:18:26,800 quite commonly, you might see something like this. I'm going to ignore everything went. 267 00:18:27,700 --> 00:18:30,300 Let's just say, Sprint out the are we still should be numb. 268 00:18:33,400 --> 00:18:34,000 Here. 269 00:18:35,900 --> 00:18:39,900 You're going to run into a problem because this thing says you're not declaring anything new 270 00:18:40,100 --> 00:18:44,900 and so here you have these set the error. So this is a trap, right? If you change from this being an actual declared 271 00:18:44,900 --> 00:18:48,900 variable to something like that, then you have a problem and you would have to do this because there's 272 00:18:48,900 --> 00:18:52,900 nothing new being declared. Here you go, there's no error 273 00:18:52,900 --> 00:18:56,700 occurring. Another annoying thing that can happen. Sometimes by 274 00:18:56,700 --> 00:19:00,700 accident is shadowing of a variable where you actually read, declare 275 00:19:00,700 --> 00:19:04,700 something, and then you don't get the expected result particularly 276 00:19:05,000 --> 00:19:05,600 this happens. 277 00:19:05,800 --> 00:19:09,900 Named return values for a function. So I'm going to demonstrate that 278 00:19:10,400 --> 00:19:14,400 is that in slice, that I've been using before? And I'm going to make a silly little function, 279 00:19:14,900 --> 00:19:18,800 which tells us whether it's empty like this. It's going to take 280 00:19:18,800 --> 00:19:22,800 in a slice of events and it's going to return a number 281 00:19:22,900 --> 00:19:26,600 and that number is going to be 1. If the slice is not 282 00:19:26,600 --> 00:19:30,600 empty or zero, if the slice is empty, obviously, realistically you would do this with a Boolean 283 00:19:30,600 --> 00:19:34,700 but let's do the following. So, we're going to go in here and we're going to say if 284 00:19:35,800 --> 00:19:39,200 The length of I is greater than zero. 285 00:19:40,600 --> 00:19:44,900 Then n equals 1, n equals 1 and we return 286 00:19:44,900 --> 00:19:48,900 from that function. So that will say if it's non empty and then we'll 287 00:19:48,900 --> 00:19:50,600 just print that result out. 288 00:19:53,500 --> 00:19:57,900 And call it with this in slice, right? So there we go. It's going to go through 289 00:19:57,900 --> 00:20:01,800 here. It's going to set and I'm going to get that value. And by default, of course, n is set to 0 290 00:20:02,000 --> 00:20:06,600 when it gets a 0 value is 0 for integer. And so, let's just run 291 00:20:06,600 --> 00:20:07,000 it. 292 00:20:08,500 --> 00:20:12,800 Oops, go, go go. Okay, 293 00:20:12,800 --> 00:20:16,800 so tells us one now. Let's suppose we're doing slightly more complicated logic and we 294 00:20:16,800 --> 00:20:20,900 decided we need to declare a variable for this. We could do the following so you can go 295 00:20:20,900 --> 00:20:24,900 in here and we're going to say something like, okay, let's just count the number of elements in that 296 00:20:24,900 --> 00:20:28,600 list, and if it's greater than 297 00:20:28,600 --> 00:20:32,500 zero, then we'll set it to 1 and we'll run. So, it's just run that program 298 00:20:34,300 --> 00:20:38,200 zero. Now, that's the big surprise you, it looks like a first glance that. 299 00:20:38,600 --> 00:20:42,300 Is the same thing everywhere and we've sort of just change this value here, 300 00:20:42,500 --> 00:20:46,600 but this end within this, if statement is not 301 00:20:46,700 --> 00:20:50,900 this in here because this thing here is a declaration. So in fact, it's allowed 302 00:20:50,900 --> 00:20:54,400 you to declare something that was already in the return parameters here, 303 00:20:55,000 --> 00:20:59,800 but as something else and there's no warning of this happening. And one of the ways is quite 304 00:20:59,800 --> 00:21:03,800 often happens is with errors. So you have an error return here. 305 00:21:04,300 --> 00:21:08,200 And then you somewhere in here, go error colon equals secure within an area. 306 00:21:08,700 --> 00:21:12,500 Because you're doing that and then lo and behold the actual error, doesn't get returned by the 307 00:21:12,500 --> 00:21:16,600 function so it's definitely a thing to work out to look out for when 308 00:21:16,600 --> 00:21:20,900 you're using named return parameters and in general, if 309 00:21:20,900 --> 00:21:24,900 you can avoid them, I think it turns out to be a bit easier to work with them. So, in this case, 310 00:21:24,900 --> 00:21:28,900 I probably would actually have just done and in like that and returned it in and I'll see if 311 00:21:28,900 --> 00:21:32,400 you have multiple of them, you can have multiple things like that. 312 00:21:38,000 --> 00:21:42,800 If you've read the effective, go web page which has lots of great information about go, there's an 313 00:21:42,800 --> 00:21:46,800 example in there something they call a leaky buffer which is a 314 00:21:46,800 --> 00:21:50,500 buffer of available. In this case buffers it is. It's a 315 00:21:50,500 --> 00:21:54,600 user's a channel to actually store them and it can be useful for 316 00:21:54,600 --> 00:21:58,900 keeping around a pool of something. In more recent versions of go, 317 00:21:58,900 --> 00:22:02,700 there's synced up pool as well. But this example really illustrates something very important 318 00:22:02,700 --> 00:22:06,800 about channels and in particular, the select statement as a, what I want to do here is just 319 00:22:06,800 --> 00:22:07,300 review it. 320 00:22:07,500 --> 00:22:11,900 Lightly. I've slightly simplified the example to look at the pertinent things. So in the 321 00:22:12,100 --> 00:22:16,900 in the Emax here, I've got something called a buffer, which could be, for example, a 322 00:22:16,900 --> 00:22:20,800 bite slice, it could be a list of strings. It could be something that you're going to 323 00:22:20,800 --> 00:22:24,700 reuse and I've got a couple of functions on it one called process and one 324 00:22:24,700 --> 00:22:28,900 called get. So get would fill the buffer with something and process would process 325 00:22:28,900 --> 00:22:32,500 whatever is in the buffer and after that we can assume the buffer can be either reused or 326 00:22:32,500 --> 00:22:36,900 discard it and to keep this leaky buffer around what they do 327 00:22:36,900 --> 00:22:37,300 is they use 328 00:22:37,400 --> 00:22:41,600 Use a channel, but here is actually not a number for jealous, a 329 00:22:41,600 --> 00:22:45,800 buffered channel. So it actually has slots for 10 and the idea is that this channel 330 00:22:45,800 --> 00:22:49,300 will keep around up to 10 of these buffers available 331 00:22:49,300 --> 00:22:53,500 and they can be reused. And so it becomes a little Q fifo queue 332 00:22:53,500 --> 00:22:57,600 of buffers. And then there's a couple of functions near which would typically 333 00:22:57,600 --> 00:23:01,900 run as go goroutines one called a worker 334 00:23:01,900 --> 00:23:05,800 and one called a controller. And the idea is that the worker has to do some work 335 00:23:05,800 --> 00:23:07,200 on buffers and 336 00:23:07,500 --> 00:23:11,600 It gets a free buffer from the available Channel or if it 337 00:23:11,600 --> 00:23:15,900 can't it will make a new one and use it. And when it's finished with that 338 00:23:15,900 --> 00:23:19,800 piece of work, it'll pass it on to another go routine. This case, 339 00:23:19,900 --> 00:23:23,700 I've just called it the controller which will then do something else with the 340 00:23:23,700 --> 00:23:27,900 buffer. So the worker is just filling buffers up and the controller is actually 341 00:23:27,900 --> 00:23:31,800 processing them. And so, what I want to do is look at how select is 342 00:23:31,800 --> 00:23:35,900 used in a particular default to implement this buffer because it's actually a very, very 343 00:23:35,900 --> 00:23:37,300 neat example of how powerful 344 00:23:37,400 --> 00:23:41,300 powerful channels and select can be. So if we look in the worker, 345 00:23:41,300 --> 00:23:45,600 you'll see that it's a continuous loop. So it's an infinite Loop and 346 00:23:45,600 --> 00:23:49,900 there's this select statement and within the select statement is this. So 347 00:23:49,900 --> 00:23:53,400 it's trying to get a buffer from the available Channel. 348 00:23:53,400 --> 00:23:57,900 And if there is one, it will use that buffer, it'll use it to it'll call get on it and it will 349 00:23:57,900 --> 00:24:01,900 then pass it on to the controller down this channel. If there isn't 350 00:24:01,900 --> 00:24:05,800 one available then what will happen is it? The select statement 351 00:24:05,800 --> 00:24:07,200 will immediately use the 352 00:24:07,400 --> 00:24:11,700 Fault. And in this case, it will make a new buffer from scratch. So it'll build a new 353 00:24:11,700 --> 00:24:15,900 buffer which can then get used and pass that on. So, in this way, if there's nothing available in 354 00:24:15,900 --> 00:24:19,800 the pool, in a van in the available Channel, it'll make a new one, otherwise it 355 00:24:19,800 --> 00:24:23,700 will pull one. Now normally when you have a select statement you're quite familiar with it 356 00:24:23,700 --> 00:24:27,800 blocking on a Case statement like this with it with a channel receiving 357 00:24:27,800 --> 00:24:31,700 it. And this is a good example of using default where if no one else is available to 358 00:24:31,700 --> 00:24:35,700 communicate available isn't ready to send us anything, then we'll drop down into 359 00:24:35,700 --> 00:24:37,300 default and really in a very 360 00:24:37,400 --> 00:24:41,500 Small amount of code here. You've got a pool, a pool of buffers, then it gets passed on to the 361 00:24:41,500 --> 00:24:45,900 controller. And the controller here is similarly looping around. It pulls 362 00:24:45,900 --> 00:24:49,900 any buffers they've been created, does whatever process needs to be done on 363 00:24:49,900 --> 00:24:53,500 it and then put them back. And this again is a very, very small select 364 00:24:53,500 --> 00:24:57,800 statement. It tries to put it onto the available Channel and if you remember the 365 00:24:57,800 --> 00:25:01,800 available channel is buffer that has space for 10 of these. So if there's room 366 00:25:02,400 --> 00:25:06,300 it will put it on, put it into there, if not 367 00:25:06,800 --> 00:25:07,200 it 368 00:25:07,500 --> 00:25:11,800 Hit the default and it will then just drop out. So this allows it to drop the buffer on the floor. So 369 00:25:11,800 --> 00:25:15,700 that point, if it doesn't manage to transmit it to available, it drops out of the 370 00:25:15,700 --> 00:25:19,800 loop and we're done. And so this, in very small amount of code gives 371 00:25:19,800 --> 00:25:23,800 you a way of pulling something could be buffers, could be any other sort 372 00:25:23,800 --> 00:25:27,700 of type that that go has using the select statement in particular, using 373 00:25:27,700 --> 00:25:31,200 default to drop out if no one else is ready to communicate. 374 00:25:35,300 --> 00:25:39,700 In this section, I'm going to talk a little bit about Anonymous struct. You'll no doubt 375 00:25:39,700 --> 00:25:43,800 familiar with structs because they're the standard thing, we use to create what might be called 376 00:25:43,800 --> 00:25:47,700 objects within go and normally we do 377 00:25:47,700 --> 00:25:51,900 at some sort of type declarations. I'm going to do a little simple type declaration like 378 00:25:51,900 --> 00:25:55,500 this. I'm going to say translation is a struct 379 00:25:55,500 --> 00:25:59,800 and it's going to have an English word. Just 380 00:25:59,800 --> 00:26:03,700 going to be a string and a French word which is a string and you created a 381 00:26:03,700 --> 00:26:05,100 translation type and then you 382 00:26:05,200 --> 00:26:08,800 Go in here and you might set up yourself. Something like this. So, 383 00:26:08,800 --> 00:26:12,700 so we're going to create ourselves a translation. 384 00:26:14,700 --> 00:26:18,600 And I'm going to say that mr. In English 385 00:26:18,800 --> 00:26:22,600 is monsieur in French and so there you go. Is simple thing and we 386 00:26:22,600 --> 00:26:26,700 could just see all that struck looks like 387 00:26:27,000 --> 00:26:29,400 use Santosh feet print it 388 00:26:34,100 --> 00:26:38,900 Let me go. So obviously there's the structure like that. There are situations which you don't 389 00:26:38,900 --> 00:26:42,700 necessarily need to actually have a named type for it. There 390 00:26:42,700 --> 00:26:46,900 may be something that you're going to use temporarily or particularly. If 391 00:26:46,900 --> 00:26:50,900 it's some sort of template, you're using a test where you might not need to declare a 392 00:26:50,900 --> 00:26:54,700 type, you might just want to do it sort of in line. So for example, 393 00:26:55,000 --> 00:26:59,900 I could actually have taken just this bit here so just the struct 394 00:26:59,900 --> 00:27:03,500 bit which is type literal and go on here. 395 00:27:04,800 --> 00:27:08,100 And on this. So here I am declaring 396 00:27:09,900 --> 00:27:13,900 something called title which, and here's the type for it, and here's 397 00:27:14,200 --> 00:27:18,900 the setup of it. So, it's just run it. I'm just going to 398 00:27:18,900 --> 00:27:22,300 delete that and then, there we go. So 399 00:27:23,200 --> 00:27:27,900 the same thing has happened here, which is that I've now got a type and you can see in the 400 00:27:27,900 --> 00:27:31,800 original one. It actually said it's this type name translation for they named it as a 401 00:27:31,800 --> 00:27:35,500 type. And here it's Anonymous is saying, this is how that particular thing has declared 402 00:27:36,000 --> 00:27:39,300 so super handy. If you want to have a list of different bits of 403 00:27:39,500 --> 00:27:43,100 Later that you're doing and you can then extend this even further, you can have 404 00:27:43,600 --> 00:27:47,500 slices of these and use them for any sort of templating. You have 405 00:27:47,800 --> 00:27:51,700 another time that Anonymous trucks as in handy as when you're reading in some data, 406 00:27:51,700 --> 00:27:55,800 especially if you're doing a little program that needs to quickly read in something and process 407 00:27:55,800 --> 00:27:59,800 it, or output it using a template for example, made a little program here which 408 00:27:59,800 --> 00:28:03,600 extends the English French thing I had before to look at some 409 00:28:03,600 --> 00:28:07,900 Jason now, obviously the Jason probably wouldn't be embedded in your program. Like this, it would probably 410 00:28:07,900 --> 00:28:09,300 be reading from a file but I've just 411 00:28:09,400 --> 00:28:13,700 Just dropped it in here and it's got a three examples. Mr. Doctor and professor translated from 412 00:28:13,700 --> 00:28:17,700 English into French and to read that into something with a 413 00:28:17,700 --> 00:28:21,900 reasonable structure. I've just created an anonymous structure here for these 414 00:28:21,900 --> 00:28:25,800 titles. So I just said, we're going to have English and French in each of these things and these 415 00:28:25,800 --> 00:28:29,900 names correspond to the Jason field names and I'm 416 00:28:29,900 --> 00:28:33,800 going to unmarshal it. I'm being a bit Naughty By ignoring the errors, but this is just an example, you 417 00:28:33,800 --> 00:28:37,900 shouldn't do that and then just output those things. And again here, I'm able 418 00:28:37,900 --> 00:28:39,300 to use the names. 419 00:28:39,500 --> 00:28:43,900 Of these elements because I've declared it here even though doesn't have a name of his own, it doesn't matter. 420 00:28:43,900 --> 00:28:47,500 It's still fully declared and if I run that it should 421 00:28:47,500 --> 00:28:51,900 unmarshal that Jason and then be able to give me the translation from English into French of those 422 00:28:51,900 --> 00:28:55,900 three names. And obviously you can imagine extending this quite easily to something where you 423 00:28:55,900 --> 00:28:59,600 use the OS file to read in a Json, whole Jason 424 00:28:59,600 --> 00:29:03,700 thing, I'm Marshall are into some temporary temporary structure and output 425 00:29:03,700 --> 00:29:07,700 it so as well as having Anonymous struct. So it's also 426 00:29:07,700 --> 00:29:09,300 possible to have an 427 00:29:09,400 --> 00:29:13,500 Interfaces and you probably use one without thinking about it at all. 428 00:29:13,900 --> 00:29:17,900 That's when you write interface like 429 00:29:17,900 --> 00:29:21,800 that you haven't actually given. The type declaration is just an interface. In this case is not only 430 00:29:21,800 --> 00:29:25,900 Anonymous, but it's got nothing in its the, the empty interface but sometimes it can actually 431 00:29:25,900 --> 00:29:29,900 be handy to use and Anonymous interface just quickly declare one and the 432 00:29:29,900 --> 00:29:33,900 sort of the funnest trick is using it within a type 433 00:29:33,900 --> 00:29:37,800 assertion. So here, there's some variable X which is interphase 434 00:29:37,800 --> 00:29:39,200 and what this is doing. 435 00:29:39,500 --> 00:29:43,600 Is saying here is a type of session and it has to support the string 436 00:29:43,600 --> 00:29:47,900 interface. It has to be have a string in it and if it doesn't 437 00:29:47,900 --> 00:29:51,400 if the type of session fails, then we'll say, oh well you know, x-axis 438 00:29:51,800 --> 00:29:55,500 account strength and you could use this for example, within a switch 439 00:29:55,500 --> 00:29:59,900 statement to determine what interfaces are available for a particular 440 00:30:00,100 --> 00:30:04,300 for a particular interface or you can use it like this to actually say, you know, at runtime. 441 00:30:04,600 --> 00:30:08,900 Well actually string is missing the most likely use of this I think, is within a 442 00:30:08,900 --> 00:30:09,300 test. 443 00:30:09,400 --> 00:30:13,200 Sweet. But it could be handy and just bear in mind that whenever you write 444 00:30:13,400 --> 00:30:17,800 interface like this, there's no reason why you can't have something with in here and actually be 445 00:30:17,800 --> 00:30:21,800 declaring temporarily an interface type with without naming 446 00:30:21,800 --> 00:30:22,100 it. 447 00:30:26,900 --> 00:30:30,900 So if you come from an object-oriented background, you might be trying to find a 448 00:30:30,900 --> 00:30:34,900 way to do subclassing and go and things like that, which, of course don't exist. Because 449 00:30:34,900 --> 00:30:38,700 the way in which it deals with objects is quite different to a lot of other 450 00:30:38,700 --> 00:30:42,700 languages but you will also have done things with struct swear structure have 451 00:30:42,700 --> 00:30:46,700 methods on them and there is a way of relating to structures 452 00:30:46,700 --> 00:30:50,800 together. So that, the say the field and the methods on those structures are actually 453 00:30:50,800 --> 00:30:54,700 combined, and that's called embedding. So what I do is I'm gonna do a little example of 454 00:30:54,700 --> 00:30:56,000 structure embedding to show her. 455 00:30:56,100 --> 00:31:00,400 Works. So what I'm going to do is I'm going to create a string class, I'm going to have my own 456 00:31:00,400 --> 00:31:04,600 sorts of strings. I'm going to call this my string, which is going to be a structure, 457 00:31:04,600 --> 00:31:08,900 it's going to store a string inside it and I'm going to declare a method on 458 00:31:08,900 --> 00:31:12,900 it which actually will output it to standard out. So we're going to just make 459 00:31:12,900 --> 00:31:15,900 ourselves. If you've got one of these my strings 460 00:31:15,900 --> 00:31:19,500 then output on it will 461 00:31:19,500 --> 00:31:23,900 print it. So we'll just do simple thing like this m dot strip, which will print it 462 00:31:23,900 --> 00:31:25,500 out and 463 00:31:26,100 --> 00:31:30,400 But also make ourselves a little function to create one new my string, which takes in a 464 00:31:30,400 --> 00:31:34,500 string and returns a my string 465 00:31:35,200 --> 00:31:39,800 with straw set to ass. So that will create that. And then we've create one of these down 466 00:31:39,800 --> 00:31:40,400 here. 467 00:31:43,000 --> 00:31:47,600 My string and sorry, I'm gonna make a new one. You my string, 468 00:31:48,900 --> 00:31:52,600 hello world, and ask it to Output 469 00:31:52,600 --> 00:31:56,900 itself. So we've got structure here, which is a symbol 470 00:31:57,200 --> 00:32:01,900 embedded Field Strip, which is a string, a way to make a new one. A way to take one of 471 00:32:01,900 --> 00:32:05,200 these, my strings and print its output, and then we'll just run it. 472 00:32:07,600 --> 00:32:07,900 Oops. 473 00:32:10,000 --> 00:32:12,700 I should remember that as you return one of these from time, 474 00:32:13,800 --> 00:32:17,800 There we go. Hello world so simple thing. Now let's suppose we wanted to take something 475 00:32:17,800 --> 00:32:21,300 that had all the functionality of my string but 476 00:32:21,700 --> 00:32:25,900 somehow extended it in a way on a simple way to do that is by embedding. 477 00:32:26,000 --> 00:32:29,000 So I'm going to do a new type which I'm going to call shouting. 478 00:32:30,000 --> 00:32:32,000 Which is also going to be a struct. 479 00:32:33,500 --> 00:32:37,900 And it's going to be able to do everything that my stream can do. So I'm going to embed it like 480 00:32:37,900 --> 00:32:41,900 this. So now I've got this shouting structure which is going to be four. Strings are all 481 00:32:41,900 --> 00:32:45,800 in uppercase I want a way of making a new one so I'm going to declare a function to do that. 482 00:32:45,800 --> 00:32:49,300 So it's a new shouting string. It's going to take in a 483 00:32:49,300 --> 00:32:52,400 string. It's going to return a shouting string. 484 00:32:54,000 --> 00:32:58,900 And it's going to be very like new new. My string above, we're going to actually uppercase 485 00:32:58,900 --> 00:33:02,200 the all the letters, so I'm going to bring in the strings package 486 00:33:02,200 --> 00:33:06,700 and I happen to know, there is a nice function for this. It's just 487 00:33:06,700 --> 00:33:10,800 check that I know the right syntax. Sorry, 488 00:33:10,900 --> 00:33:13,800 let's go doc strings here and if we look 489 00:33:13,800 --> 00:33:17,900 there's a nice function here which is called to Upper so that 490 00:33:17,900 --> 00:33:21,900 will uppercase the stream for me. That's great. So I'm going to use that so 491 00:33:21,900 --> 00:33:22,600 I'm going to come in here. 492 00:33:22,800 --> 00:33:26,500 And I'm going to Claire get myself in my 493 00:33:26,500 --> 00:33:27,000 string. 494 00:33:30,100 --> 00:33:32,200 So we can count, sorry, shouting string. 495 00:33:35,800 --> 00:33:38,900 Like that, I'm going to set its string to be 496 00:33:41,100 --> 00:33:45,800 the upper casing of that thing, and I'm going to return so that should be like 497 00:33:45,800 --> 00:33:49,800 that and I'm going to change this to be a shouting string here. So 498 00:33:50,400 --> 00:33:54,700 if we just take a look at this, what's happened to you is, I've created a new one of these shouting 499 00:33:54,700 --> 00:33:58,800 structures, which embeds my string, which contains this strut 500 00:33:59,600 --> 00:34:03,900 and I'm going to set it to be the upper casing of this thing, the shouting part, 501 00:34:03,900 --> 00:34:05,400 and return it. And then 502 00:34:05,500 --> 00:34:09,800 I'm going to create a new shouting string, so this will be of this type and I'm 503 00:34:09,800 --> 00:34:13,800 going to call output and output is actually a method on the parent if you like all there 504 00:34:13,800 --> 00:34:17,900 isn't really a sense of a parenting go so for a lucky I should put her on 505 00:34:17,900 --> 00:34:21,900 that and there you go. Hello world comes out shouting. So what we've done there is we've 506 00:34:21,900 --> 00:34:25,900 embedded one structure to another and it inherited both the fields that 507 00:34:25,900 --> 00:34:29,800 were in it and had access to them and also the methods that 508 00:34:29,800 --> 00:34:33,900 were defined on it. And interesting question is, what happens if you try to define a 509 00:34:33,900 --> 00:34:35,500 method with the same name? 510 00:34:35,500 --> 00:34:39,700 I'm named. Let's just try this out. So I'm going to say, oh, I'm shouting. 511 00:34:43,000 --> 00:34:47,900 And do output. And I'm going to change the slider. Just so we know which one is going to say really loud. 512 00:34:51,900 --> 00:34:55,000 Like that. Right? And there's a line feed in there 513 00:34:56,600 --> 00:35:00,500 and let's run this. And so now in a sense you've 514 00:35:00,500 --> 00:35:04,900 overridden output it now because you're using this one is so it hasn't there's 515 00:35:04,900 --> 00:35:08,800 no error here. You're defining a method on that particular type and 516 00:35:08,800 --> 00:35:12,900 because there was one in the parent if you like it's actually been overridden and now 517 00:35:12,900 --> 00:35:16,900 you're calling calling that particular one. So that's really embedding. It's a useful 518 00:35:16,900 --> 00:35:20,900 thing. Sometimes the most common use of embedding you'll see is to include a 519 00:35:20,900 --> 00:35:21,600 mutex 520 00:35:21,700 --> 00:35:25,900 In a structure, I'll just show you quickly what that would look like. And I'm actually going to 521 00:35:25,900 --> 00:35:29,500 need a new text for this, but there might be a time you could just do that. 522 00:35:32,000 --> 00:35:36,600 Sync, mutex, you've included everything about a mutex, all of its methods, within a, my 523 00:35:36,600 --> 00:35:40,800 string. And then if you want to lock a my string and unlock it, let's suppose that 524 00:35:40,800 --> 00:35:44,700 some reason we need to do that. You could go into here and you can say 525 00:35:44,700 --> 00:35:47,100 hello Dot Lock. 526 00:35:48,500 --> 00:35:50,000 And could unlock it again 527 00:35:53,300 --> 00:35:57,800 and notice here. This this is a shouting string which embedded in my string, 528 00:35:57,800 --> 00:36:01,600 which embedded a sink mutex, but that will allow us to lock and 529 00:36:01,600 --> 00:36:05,900 unlock without having to explicitly talk about a mutex. Some people, 530 00:36:05,900 --> 00:36:09,700 you'll sometimes see something like this mu text, then you've got to go a 531 00:36:09,700 --> 00:36:13,400 Hello dot m dot and it becomes a bit of a Mess by embedding. You, 532 00:36:13,800 --> 00:36:17,900 you make it protected by a mutex also, if you stick it at the 533 00:36:18,200 --> 00:36:22,500 Pierre. And it makes it very clear. Clear? When you're reading the code, that this 534 00:36:22,500 --> 00:36:26,800 particular structure actually has a mutex, which protects it and obviously, there's that means there must be 535 00:36:26,800 --> 00:36:30,800 something that needs needs protecting in it, or it's being used in some interesting concurrent way. 536 00:36:35,400 --> 00:36:39,800 If you spend any time looking at the Go standard packages, you'll come 537 00:36:39,800 --> 00:36:43,700 across some time to time functions which have the name must in them 538 00:36:44,000 --> 00:36:48,800 and these are actually typically methods that will cause a panic or E 539 00:36:49,400 --> 00:36:53,700 runtime hard failure. If something goes wrong and they're usually 540 00:36:53,700 --> 00:36:57,600 using the case that this should never ever happen and it can be quite 541 00:36:57,600 --> 00:37:01,900 useful to do this because what you're doing is you're failing as fast as possible. And a great 542 00:37:01,900 --> 00:37:04,900 example of this is must compile in the wregget. 543 00:37:05,000 --> 00:37:09,900 Package typically within a program, you'll have a lot of fixed regular expressions. And 544 00:37:09,900 --> 00:37:13,700 if you compile them, which you may want to do for Speed the actual standard, 545 00:37:13,700 --> 00:37:17,800 regrets compile function returns an error, and you need to check the error in need to handle the error. But 546 00:37:17,800 --> 00:37:21,700 if you know, for certain that the regular expression will always compile this, something new you 547 00:37:21,700 --> 00:37:25,900 fixed at at the time, you rode the program, then you can 548 00:37:25,900 --> 00:37:29,700 use must compile. So let's just do a little example of that, just to see how that works. 549 00:37:30,000 --> 00:37:34,700 So first of all, you go doc to have a look at reg ex compile. 550 00:37:36,000 --> 00:37:40,800 So this is compile function, which allows you to take an expression and it 551 00:37:40,800 --> 00:37:44,900 gives you an, a regular expression and an error. So, we just bring in the 552 00:37:44,900 --> 00:37:48,700 regular expression package here and we 553 00:37:48,700 --> 00:37:52,400 try to compile something. So, let's say a regular expression error 554 00:37:57,200 --> 00:38:01,500 And in here will be expected to look at that error. So we're going to do something 555 00:38:01,500 --> 00:38:05,800 simple like this. Needs to match. 029 556 00:38:06,400 --> 00:38:10,900 this needs to match a number. I this is a perfectly valid regular expression and if error is 557 00:38:10,900 --> 00:38:14,600 not equal to nil, then we're going to say something horrible. So let's just do this. 558 00:38:14,600 --> 00:38:18,900 Fatal error has occurred, right? Bad regular expression. The when you're writing this sort of the, 559 00:38:18,900 --> 00:38:22,600 you get the feeling like you're writing something which is never ever going to get executed because 560 00:38:22,900 --> 00:38:26,800 this this better compiled, but those we have to handle the error 561 00:38:27,300 --> 00:38:31,900 So, let's just do this and we'll bring in the log package, so that's 562 00:38:31,900 --> 00:38:35,900 being being logged. And then obviously, we could use that regular expression 563 00:38:35,900 --> 00:38:39,700 to match something so let's just look at that. So if we look in here 564 00:38:40,500 --> 00:38:44,900 go dark rexqq match, there's this match function, which allows 565 00:38:44,900 --> 00:38:48,600 you to do it like that, or you can do it on an existing regular 566 00:38:48,600 --> 00:38:51,800 expression, and there's also match string. 567 00:38:52,700 --> 00:38:56,000 That's the one we're going to use when a match the string so I'm going to say like this 568 00:38:58,400 --> 00:39:00,000 does it match just do this? 569 00:39:01,800 --> 00:39:02,700 Truth value. 570 00:39:05,900 --> 00:39:09,200 Does it regular expression match the string? Hello. 571 00:39:10,600 --> 00:39:14,700 Which I'm pretty sure it won't match because there's no numbers in 572 00:39:14,700 --> 00:39:18,900 it. Don't rub some and we don't use, 573 00:39:19,200 --> 00:39:23,600 we tell format, thank you, go for reminding me of that. Okay. Now, it doesn't 574 00:39:23,600 --> 00:39:27,800 match but also you'll notice that this thing never get executed, and it was 575 00:39:27,800 --> 00:39:31,600 completely and utterly useless in this situation and I was just check this really 576 00:39:31,600 --> 00:39:33,600 works. Let's just say hello, one, two, three, 577 00:39:34,900 --> 00:39:38,900 That's right that get out. That does match. Okay, so this is obviously working fine. Now, what you'd 578 00:39:38,900 --> 00:39:42,800 really like to do is eliminate writing useless code like this is never going to get executed, 579 00:39:43,000 --> 00:39:47,900 and also, you want to detect a compilation error early because if 580 00:39:47,900 --> 00:39:51,900 you, if this is wrong, if this thing doesn't compile correctly, then 581 00:39:51,900 --> 00:39:55,900 it's going to indicate that you have really screwed up badly. And this is something 582 00:39:55,900 --> 00:39:59,800 that should have been fixed long before. Let's see if I can make it fail early, you go see, 583 00:40:00,200 --> 00:40:03,900 there's a bad if you did that, then it's a program here. Another way to do this in fact is to 584 00:40:04,000 --> 00:40:08,500 Use must compile. And the best way to do that is something like this. So let's just call this thing 585 00:40:08,500 --> 00:40:12,800 match number and we're going to go up here. I'm going to say well I should let's look it up in the 586 00:40:12,800 --> 00:40:13,500 old go, dog. 587 00:40:17,600 --> 00:40:21,600 It's right, it's right to do that. Okay, 588 00:40:21,800 --> 00:40:25,500 so must compile returns a reggae it's just like 589 00:40:25,500 --> 00:40:29,300 before but it will also Panic if something's wrong. So 590 00:40:29,800 --> 00:40:33,900 let's use that as do must compile and let's 591 00:40:33,900 --> 00:40:35,900 make the same error, that's below. 592 00:40:38,200 --> 00:40:42,400 And then we can eliminate all this code and we can use match number here. 593 00:40:45,600 --> 00:40:48,400 so this is way cleaner than what we had before, and 594 00:40:50,000 --> 00:40:54,800 As usual. I forgot to 595 00:40:54,800 --> 00:40:58,600 remove something to go with Som know about bam Panic. So the program 596 00:40:58,600 --> 00:41:02,700 refuses to execute because in fact this compilation fails and it, the 597 00:41:02,700 --> 00:41:06,500 error is quite clearly shown their this is what's wrong with it. And so if I then 598 00:41:06,500 --> 00:41:07,900 fix that, 599 00:41:09,300 --> 00:41:13,500 Andre run my program. There you go. It works! Fine. So if there are 600 00:41:13,500 --> 00:41:17,900 must something very things available, there's one of the template packages well, when you're taking a 601 00:41:17,900 --> 00:41:21,800 template, which, you know, is fixed, and you know, will actually work. Then use it because it 602 00:41:21,800 --> 00:41:25,300 makes the code, much, much cleaner. You don't have to be if they're erring all the way through. 603 00:41:29,900 --> 00:41:33,600 So, you've probably used the defer statement, for example, to 604 00:41:33,600 --> 00:41:37,900 close a file. When a function exits, especially when you have error conditions, you want to make sure 605 00:41:37,900 --> 00:41:41,700 it gets closed, or if you use the HTTP package, to make sure the 606 00:41:41,700 --> 00:41:45,200 body of a request is closed, and it's a very, very useful 607 00:41:45,200 --> 00:41:49,800 function within go. But there are some cases where it can be a little bit 608 00:41:49,800 --> 00:41:53,800 tricky with a some couple little pitfalls. So I'm just going to review the use of defer 609 00:41:53,800 --> 00:41:57,800 and look at the order in which twofers happen. The way in which 610 00:41:57,800 --> 00:41:58,900 the arguments and the 611 00:41:59,300 --> 00:42:03,800 Defer is are evaluated and also a little nice thing you can do around error handling. 612 00:42:03,800 --> 00:42:07,300 So let's just do some basic defer to start with. Let's make ourselves 613 00:42:07,300 --> 00:42:11,200 a simple little function. Let's call it do 614 00:42:11,200 --> 00:42:15,300 and we'll make it defer a few things so it's going to print out. 615 00:42:16,700 --> 00:42:20,900 This is the first defer, my guess could do 616 00:42:20,900 --> 00:42:23,600 three to First going to look for anything. Very interesting, this function 617 00:42:25,800 --> 00:42:26,700 and we'll call it. 618 00:42:28,700 --> 00:42:29,600 And we'll run it. 619 00:42:30,800 --> 00:42:34,700 So third, second first. So as you can see, first of all, 620 00:42:34,900 --> 00:42:38,800 the defer order is strictly backwards. So the last of fur goes 621 00:42:38,800 --> 00:42:42,700 first, I was very useful. If, for example, you opened a database and then you need to 622 00:42:42,700 --> 00:42:46,800 query on the database. When you opened up the query and you want to make sure 623 00:42:46,800 --> 00:42:50,600 that the roads are closed before the database handle is closed. So that 624 00:42:50,600 --> 00:42:54,700 happens in that order. Now, the other thing that happens 625 00:42:54,700 --> 00:42:58,800 within defer, is that the right hand side, which is always a function 626 00:42:58,800 --> 00:43:00,100 call, it's always a function 627 00:43:00,900 --> 00:43:04,900 Gets evaluated and it gets evaluated in two different ways, 628 00:43:05,000 --> 00:43:09,700 the arguments to the defer get devalue ated, when the defer statement 629 00:43:09,700 --> 00:43:13,700 appears and the actual function gets evaluated when the de first, it 630 00:43:13,700 --> 00:43:17,900 actually executes. So, to illustrate that, I'm going to add in a number in here, 631 00:43:17,900 --> 00:43:21,800 was going to call it and just an integer. And I'm 632 00:43:21,800 --> 00:43:23,400 actually going to print it each time. 633 00:43:26,600 --> 00:43:27,500 so, if we go in here, 634 00:43:33,300 --> 00:43:37,800 So this shouldn't be any surprise in what happens here. Let's just make sure 635 00:43:37,800 --> 00:43:38,800 that when around that 636 00:43:41,700 --> 00:43:43,200 Freshly printed it would help, wouldn't it? 637 00:43:46,900 --> 00:43:50,900 Okay, so one because n is there. So let's now try changing the 638 00:43:50,900 --> 00:43:53,800 value of n here. 639 00:43:55,300 --> 00:43:59,400 So it's changed. Let's run it. So now what you can see is that 640 00:43:59,700 --> 00:44:03,300 and has taken on the value to here 641 00:44:03,900 --> 00:44:07,700 and one here, because this part the arguments to the 642 00:44:07,700 --> 00:44:11,900 function were evaluated at the time that the first statement happened, so you can see 643 00:44:11,900 --> 00:44:15,800 the End changed value, but it didn't change what was in here. 644 00:44:16,200 --> 00:44:20,800 It's quite a common Pitfall to think that n here because it's changed 645 00:44:20,800 --> 00:44:24,800 here before the defer statement actually executed means that this will be 646 00:44:25,100 --> 00:44:29,800 And when in fact, this part was evaluated before hand, that's actually part of the reason why you 647 00:44:29,800 --> 00:44:33,900 quite often, see people do defer Funk with no arguments and then they do all the 648 00:44:33,900 --> 00:44:37,700 evaluation within their. So that gives you in a 649 00:44:37,700 --> 00:44:41,500 defer, you have to remember that that the arguments are evaluated 650 00:44:41,600 --> 00:44:45,800 beforehand and now this is particularly important if you're looking at errors. So if 651 00:44:45,800 --> 00:44:49,900 errors can occur throughout and then you're using it fur and you're looking at the error 652 00:44:49,900 --> 00:44:53,500 value, you need to be a little bit careful. But one, nice thing you can do is you can 653 00:44:53,500 --> 00:44:54,900 use an anonymous functional. 654 00:44:55,100 --> 00:44:59,800 Like this to actually look at the error so let's just do this. Imagine there's an error that can occur in here. 655 00:45:01,800 --> 00:45:05,600 Like this. And I'm going to make this function to fur, which is going to on 656 00:45:05,600 --> 00:45:07,000 exit. Look at that error. 657 00:45:09,400 --> 00:45:13,900 And if there is an error, I'm going to use log. I'm going to say, you know, I don't just 658 00:45:13,900 --> 00:45:16,800 use print out an error occurred. 659 00:45:23,900 --> 00:45:25,100 And so, we go through. 660 00:45:27,200 --> 00:45:31,900 And so, because this is anonymous, there's no this. Nothing to evaluate. Here's 661 00:45:31,900 --> 00:45:35,900 this whole thing. Will go to evaluate it on exit. And so now if I go into here and I say 662 00:45:35,900 --> 00:45:39,200 error equals something bad thing 663 00:45:39,200 --> 00:45:40,100 happened. 664 00:45:42,600 --> 00:45:44,400 I'm going to clear the errors Library. 665 00:45:46,400 --> 00:45:50,300 Then I'm going to need log as well. I don't know. But it looks like 666 00:45:51,400 --> 00:45:55,700 then let's run that bam, I need to remember that we 667 00:45:55,700 --> 00:45:57,500 actually have to call it. 668 00:45:58,700 --> 00:46:02,700 They're an error occurred, even though Arrow was set after the defer 669 00:46:02,700 --> 00:46:06,900 because it goes. Now, what you're doing is you're looking inside there. So this can be very, very helpful, 670 00:46:06,900 --> 00:46:10,700 especially in error situations, where you may want to login error, you don't have to make 671 00:46:10,700 --> 00:46:14,200 sure every instance where you've set the area, you're actually logging it. 672 00:46:15,100 --> 00:46:19,600 The other thing you can do with errors and this is kind of Handy, is format actually has a nice 673 00:46:19,600 --> 00:46:23,700 function called error F, which will actually create an error. So you can do 674 00:46:23,700 --> 00:46:27,800 that and that voice you having to bring in the arrows package. And of course, because it's 675 00:46:27,800 --> 00:46:28,000 an 676 00:46:28,500 --> 00:46:32,600 F function allows you to give a lot more information in there, just like you will be the 677 00:46:32,600 --> 00:46:36,600 Sprint are for printf, so that's really the basics for defer, 678 00:46:37,400 --> 00:46:41,600 just remember that the arguments get evaluated immediately, the function itself gets 679 00:46:41,600 --> 00:46:43,800 evaluated, when the defer executes 680 00:46:48,400 --> 00:46:52,600 So as you will obviously know as a programmer and you spent half your time dealing with errors and error 681 00:46:52,600 --> 00:46:56,900 handling and in go of course there is an error type and 682 00:46:56,900 --> 00:47:00,600 there are various different ways of creating errors, obviously 683 00:47:00,700 --> 00:47:04,900 most of the time you may well be just looking at errors coming back from functions you're calling. But 684 00:47:04,900 --> 00:47:08,900 occasionally you need to generate your own and there are a few different ways of doing that 685 00:47:09,000 --> 00:47:13,100 and so I'm going to take you through how to generate errors. So obviously 686 00:47:13,800 --> 00:47:17,600 within go. There's this built-in type error, which I'm going to look at in a minute, is 687 00:47:17,600 --> 00:47:18,100 actually an 688 00:47:18,300 --> 00:47:22,900 Face type and it has just has one function it. But for the moment, let's pretend it. 689 00:47:22,900 --> 00:47:26,500 We don't know the details. So obviously we have an error and if we want to 690 00:47:26,500 --> 00:47:30,900 generate an error with it, a name, a string in it. There are two really simple ways to do it. So we 691 00:47:30,900 --> 00:47:34,700 can do the following. We can use the errors package and that 692 00:47:34,700 --> 00:47:38,200 allows us to do something like, you know, fatal 693 00:47:39,200 --> 00:47:40,500 exception or something. 694 00:47:41,900 --> 00:47:44,100 and if I bring in errors, 695 00:47:45,700 --> 00:47:48,900 then suppose that's happened and I want to print it out 696 00:47:52,000 --> 00:47:56,900 Let's just do that. So I've created something like that and go 697 00:47:56,900 --> 00:48:00,800 run, okay, so there you go. So there's that particular error and this is an 698 00:48:00,800 --> 00:48:04,800 error, it could be return from a function requires an error, it is of the 699 00:48:04,900 --> 00:48:08,800 of the error type. Now it's a little bit inflexible because it just takes a 700 00:48:08,800 --> 00:48:12,800 string and maybe you want to include some more information in there and within the format 701 00:48:12,800 --> 00:48:16,300 package there's a nice little function. It's just ask your doctor about it. 702 00:48:18,000 --> 00:48:22,600 Our F which is a standard print F Type function and it returns an 703 00:48:22,600 --> 00:48:26,500 arrow returns something that satisfies the air interface. So we could use that 704 00:48:26,500 --> 00:48:30,700 perhaps so you can do error F. And then we can do you know 705 00:48:31,400 --> 00:48:35,600 a problem occurred with and something 706 00:48:35,900 --> 00:48:39,700 and thing and let's just say thing 707 00:48:39,800 --> 00:48:42,100 equals here, my module 708 00:48:44,700 --> 00:48:48,300 So that's just going to create a standard thing and I'm no longer need the errors package. 709 00:48:49,600 --> 00:48:53,700 And run that it from Coburn my module. So that gives you a way to create an error 710 00:48:53,700 --> 00:48:57,600 string. That's great. If you want to pass around, just a 711 00:48:57,600 --> 00:49:01,900 string and you don't need the rest of your program to know, what type of error it is. 712 00:49:01,900 --> 00:49:05,700 But sometimes it's handy to know the type of error and the way 713 00:49:05,700 --> 00:49:09,900 that the simplest way to do that within within one of your packages is to declare 714 00:49:09,900 --> 00:49:13,800 something. So we could say so because 715 00:49:13,800 --> 00:49:17,900 they are ER bad start up this something 716 00:49:17,900 --> 00:49:19,100 failed on Startup, 717 00:49:19,300 --> 00:49:23,600 Then just do it within. Here we go. Areas new failed to start 718 00:49:23,600 --> 00:49:24,400 correctly. 719 00:49:26,800 --> 00:49:30,500 And then obviously, that thing is an error. I need the errors package again. 720 00:49:33,400 --> 00:49:36,300 And I can go in here and just go error. Equals error, bad 721 00:49:37,100 --> 00:49:38,000 startup. 722 00:49:41,100 --> 00:49:45,300 Whoops, when the my braces here, 723 00:49:46,200 --> 00:49:50,900 right? So are Phone Stock correctly, the other good thing is I can actually do things like 724 00:49:50,900 --> 00:49:51,300 this. 725 00:49:55,100 --> 00:49:59,900 I'm gonna do something that's actually be. There's just she cheat and say, we're going 726 00:49:59,900 --> 00:50:00,900 to ignore that. 727 00:50:06,300 --> 00:50:10,900 Right? So no longer prints that because it knows that particular thing and this is extremely useful if those 728 00:50:10,900 --> 00:50:14,900 errors are declared with a capital letter at the beginning so I public in some 729 00:50:14,900 --> 00:50:18,200 package so if you have some package where you have some errors, you want to work with, 730 00:50:18,500 --> 00:50:22,600 then you can do these comparisons and know the type of the error without having to do, 731 00:50:22,600 --> 00:50:26,800 silly things with string matching. So that's that's the Tuda. Basic 732 00:50:26,800 --> 00:50:30,900 thing now error itself is actually just an interface 733 00:50:31,200 --> 00:50:35,900 and in fact it's just an interface like this, it's type error interface. 734 00:50:36,600 --> 00:50:40,300 And it has an error function and the error function Returns the string. 735 00:50:41,200 --> 00:50:45,600 So anything that has an error function is an error, so we could take any sort of 736 00:50:45,600 --> 00:50:49,700 type and add that to it in. This can be particularly useful if you wanted to 737 00:50:49,700 --> 00:50:53,900 augment something with extra information about the type of error so 738 00:50:53,900 --> 00:50:57,700 we could have a type. So my error 739 00:50:58,900 --> 00:51:02,800 and for example, maybe we will, maybe 740 00:51:02,800 --> 00:51:06,200 there's a there's obviously a string which tells us what the error is but there also might 741 00:51:06,400 --> 00:51:10,900 Be a time when it happened. So we could bring in the time 742 00:51:10,900 --> 00:51:14,600 package and we can say, when this error occurred and then we come into here and we could say, 743 00:51:14,600 --> 00:51:18,200 right, I just need to declare error on that. So 744 00:51:19,400 --> 00:51:21,700 my error has to have an error string. 745 00:51:23,000 --> 00:51:27,900 And it has to return a string and then we could use that to 746 00:51:27,900 --> 00:51:31,400 printf out when this occurred. So we could go in here and say 747 00:51:32,200 --> 00:51:36,800 this that happened at and then we can say when 748 00:51:37,900 --> 00:51:40,700 so it go in here, I could say when 749 00:51:43,800 --> 00:51:44,400 and, 750 00:51:45,800 --> 00:51:49,100 We'll go into here and there will create one of these. Let's just do that. 751 00:51:50,800 --> 00:51:53,700 So Eric only calls my error. 752 00:51:55,800 --> 00:51:57,900 And we can set it string, too. 753 00:52:00,400 --> 00:52:04,800 Bad thing when equals timed on now. 754 00:52:05,900 --> 00:52:09,800 So we've got all those things in place and then this thing is in 755 00:52:09,800 --> 00:52:13,200 fact, an error. So we could return it whenever the error type is 756 00:52:13,200 --> 00:52:17,300 needed and we could use it within other functions that require errors. So 757 00:52:17,700 --> 00:52:21,700 what's good here is that as soon as you declare that you are an error within go and you can create 758 00:52:21,700 --> 00:52:25,600 something that's much more structured than you might do just with a simple 759 00:52:25,600 --> 00:52:29,100 string. And you'll see this used quite a lot within the go standard. 760 00:52:29,200 --> 00:52:33,900 Particularly in OS package with things like path errors and other types of Errors, which defined which 761 00:52:33,900 --> 00:52:37,700 have more information. So the error function needs to actually return a 762 00:52:37,700 --> 00:52:41,800 string. So we fix that to use printf and now that we've got this error we 763 00:52:41,800 --> 00:52:45,900 can print it out loud with might do with any other error so I'm just going to Simply do 764 00:52:45,900 --> 00:52:49,400 that. That's going to call the appropriate error function to get a string. 765 00:52:49,700 --> 00:52:53,700 And what we'll see is that we get bad thing which was 766 00:52:53,700 --> 00:52:57,700 the name of the error and actually the time we recording this right now 767 00:52:58,200 --> 00:52:58,700 and so 768 00:52:59,100 --> 00:53:03,700 are you've got a compound error if you like which has more information than just one created with format dot 769 00:53:03,700 --> 00:53:05,400 error are for errors, dot new