WEBVTT 0 00:00.420 --> 00:05.960 Welcome! Let's talk about the nil value. nil is a predeclared identifier. 1 00:06.000 --> 00:12.580 Just like the other predeclared identifiers such as True, False lung function int, float64 and so on. 2 00:12.630 --> 00:21.090 so it can be used everywhere without importing any package. simply means nothing or something 3 00:21.150 --> 00:26.600 undefined, uninitialized and so. let's take a look at how it's called. 4 00:26.620 --> 00:34.780 In other popular languages, for example in Javascript it is known as null or in Python it is None 5 00:34.820 --> 00:37.620 in Java it is null 6 00:37.640 --> 00:43.850 in Ruby as in Go it is nill 7 00:44.010 --> 00:49.670 So if we know about one of these languages you already know what I'm talking about. 8 00:49.830 --> 00:51.630 But if you don't, no worries! 9 00:51.630 --> 00:57.080 It's actually a very simple concept to understand. it will be especially crystal clear 10 00:57.120 --> 00:59.940 when I'll be talking about the pointers afterwards. 11 01:00.830 --> 01:08.380 Basically nil just means that there is no particular value exists, but nil itself is a value. 12 01:08.420 --> 01:13.110 It's a special value which says that there is no value. 13 01:13.360 --> 01:17.630 You know already that in Go go every uninitialized variable gets a zero value. 14 01:17.830 --> 01:26.500 Right? there is zero value for all the pointer based types like pointers, slices, maps, interfaces and channels 15 01:26.590 --> 01:28.120 are nil. 16 01:28.160 --> 01:33.770 That means values belong to those types are not yet initialized to some proper value. 17 01:33.930 --> 01:38.600 In Go new value can be untyped and typed depending on the context. 18 01:38.690 --> 01:41.250 You'll see about that later. 19 01:41.460 --> 01:45.380 You also learn about those pointer backed types afterwards. 20 01:45.540 --> 01:56.180 However, It is good to know now when nil is used for, ok. OK, In Go new value is also extensively used for error handling. 21 01:56.270 --> 01:59.850 For example let's say that a function returns an error value. 22 01:59.930 --> 02:04.910 I'm going to talk about what an error value means in the next lecture so when you call that function and when it 23 02:04.910 --> 02:07.610 returns a nil value as the error value. 24 02:07.640 --> 02:13.370 Then it means that there wasn't an error and the function call was successful or if the error wasn't 25 02:13.370 --> 02:16.330 nil then it means that there was an error. 26 02:16.410 --> 02:18.170 To understand what I mean by this. 27 02:18.230 --> 02:20.710 Let's take a look at the simple code snippets here. 28 02:22.240 --> 02:25.720 As you can see here I'm calling a function named do. 29 02:26.070 --> 02:28.030 And it returns an error value. 30 02:28.090 --> 02:31.370 So I am saving it into variable name "err". 31 02:31.470 --> 02:34.460 So what should I do with this error value. 32 02:34.530 --> 02:37.680 Let's check it out. 33 02:37.720 --> 02:44.780 Here I'm just saying that when err variable isn't nill, then lets run the code inside the if statement here. let's 34 02:44.820 --> 02:50.470 say that the err variable wasn't nill so the code inside the if statement will be executed. 35 02:50.550 --> 02:58.170 Right, this means that there is an initialized value inside the error variable because it wasn't nill 36 02:58.260 --> 03:01.120 the "do" function here has initialized that err variable. 37 03:01.130 --> 03:04.750 It's an error value and then it returned it. 38 03:04.750 --> 03:09.620 So since there was an error this means that calling "do" function was unsuccessful. 39 03:10.040 --> 03:12.210 OK, it couldn't do its job. 40 03:12.350 --> 03:13.660 There was a problem. 41 03:13.800 --> 03:14.430 That's why 42 03:14.440 --> 03:17.190 It has returned and non-nil err value. 43 03:17.910 --> 03:22.260 So if that's the case then I need to handle error writing inside the if statements. 44 03:22.260 --> 03:26.910 OK, then maybe I can lock that error or I can terminate from the program. 45 03:28.090 --> 03:31.060 Otherwise let's say that the err variable was nil. 46 03:31.300 --> 03:38.200 So that means there was no error! "do" function was successful so it returned a nil error value. 47 03:38.200 --> 03:45.280 This means that I can continue executing my program because there wasn't an error. 48 03:45.280 --> 03:47.680 So in summary, when there is an error. 49 03:47.680 --> 03:49.670 You should always handle it. 50 03:49.770 --> 03:55.380 one common way of doing it is just by using a simple if statement as in here. 51 03:55.480 --> 03:57.340 Then you can take proper actions. 52 03:57.340 --> 04:01.390 For example you can terminate from the program or if there wasn't an error. 53 04:01.390 --> 04:06.760 You can happily continue executing the rest of the statements in your program. 54 04:06.830 --> 04:07.430 All right. 55 04:07.430 --> 04:13.190 This was a quick introduction to nil values and handling errors in Go. It is very normal if you don't 56 04:13.220 --> 04:14.770 understand it now. 57 04:14.830 --> 04:16.030 But with further examples. 58 04:16.040 --> 04:18.940 nil and error handling will be much more clear. 59 04:19.010 --> 04:21.370 No worries! In the next lecture. 60 04:21.380 --> 04:26.030 I'm going to talk about error values and how to handle them using a real world code. 61 04:26.030 --> 04:27.190 Thank you for watching! Bye Bye!.