WEBVTT 0 00:04.810 --> 00:11.680 Welcome! In this lecture, I'm going to show you when to use a short-variable declaration. 1 00:11.920 --> 00:17.880 I've decided to add the lecture about this since I've seen a lot of people curious about it. 2 00:17.910 --> 00:19.180 OK, let's get started! 3 00:20.710 --> 00:25.120 Here are a few tips for you about when to use a variable declaration 4 00:25.140 --> 00:27.160 instead of a short declaration. 5 00:27.750 --> 00:35.260 Here's the first tip: If you don't know the initial value for a variable then use the (normal) declaration. 6 00:35.290 --> 00:37.930 This is a convention among Go community. 7 00:37.930 --> 00:44.260 I recommend you to follow it. So that everyone can understand your code more easily. By the way, 8 00:44.270 --> 00:48.380 when I say "declaration", I mean the (normal) variable declaration. 9 00:48.690 --> 00:56.480 So, let me show you an example for this in the editor. Let's say that you want to track game scores 10 00:56.580 --> 01:00.500 and you don't know the initial score. 11 01:00.510 --> 01:04.110 To do that, don't use a short declaration here. For example, 12 01:04.110 --> 01:10.110 this is not good. Instead, it's better to use a declaration here. 13 01:10.340 --> 01:13.180 You can do so like this. 14 01:13.220 --> 01:20.040 This will be initialized to its zero value automatically which is zero. So, others can know understand 15 01:20.500 --> 01:22.480 that you don't know this variable's 16 01:22.500 --> 01:27.980 initial value by looking at this declaration. 17 01:28.050 --> 01:29.540 Here's the second tip. 18 01:29.630 --> 01:36.950 Use the normal declaration or variable declaration when you need a package scoped variable. 19 01:37.090 --> 01:44.010 For example, here, at the package scope, you can only use a (normal) declaration, you can't use a short declaration. 20 01:45.190 --> 01:47.120 For example, you can't use this. 21 01:50.140 --> 02:02.460 But you can do it with a declaration like this. Alright. 22 02:02.470 --> 02:06.760 So, here's the third tip: Use the declaration 23 02:06.760 --> 02:11.650 when you want to group variables together for greater readability. 24 02:11.650 --> 02:18.030 So, if you want to show other developers that some variables are related to each other, if they go hand in 25 02:18.030 --> 02:23.610 hand, then use the normal declaration to show that to them (other developers). 26 02:23.620 --> 02:27.340 Let's say that you have created a video player. 27 02:27.440 --> 02:33.160 You can do that using a multiple variable declaration like this. 28 02:35.410 --> 02:38.090 Let's declare a string variable named video. 29 02:38.120 --> 02:47.110 This is the name of the video. And, let's declare two int variables named duration and current. 30 02:47.140 --> 02:54.890 Note that, I have separated the duration and current variables from the video variable. So that, I'm 31 02:54.890 --> 03:02.760 being grouping them here for better readability. Since the duration and current are related. 32 03:02.810 --> 03:07.940 duration is the duration of the video and, the current is the current second a playing video. 33 03:07.950 --> 03:08.320 duration is the duration of the video and, the current is the current second a playing video. 34 03:12.270 --> 03:17.940 Here are a few other tips for you when to use a short variable declaration. 35 03:18.820 --> 03:25.300 Let me start by telling you that short declaration is the most used and preferred declaration style in Go. 36 03:25.310 --> 03:29.330 As gophers, we like to write concise code. 37 03:29.530 --> 03:34.920 So we prefer a short declaration over the variable declaration most of the time. 38 03:34.980 --> 03:36.400 Here is the first tip. 39 03:36.420 --> 03:42.510 This is the opposite of the declaration advice. If you know the initial value of a variable, then 40 03:42.510 --> 03:45.940 use a short declaration. 41 03:45.940 --> 03:52.740 Let's say that you want to calculate the area of a room and you know it's a width and height already. 42 03:54.470 --> 03:58.050 Then, don't declare it like this. 43 03:58.130 --> 04:01.670 Instead, use a short declaration like this. 44 04:03.570 --> 04:04.240 OK. 45 04:04.430 --> 04:06.110 And here is the second tip. 46 04:06.500 --> 04:09.920 As I've already demonstrated to you in the previous example, 47 04:10.120 --> 04:11.620 use the short declaration 48 04:11.620 --> 04:16.580 when you want to keep the code concise and easy to read. 49 04:16.720 --> 04:23.770 Here is the last tip. As you've seen, only with a short declaration, you can both declare and assign 50 04:23.770 --> 04:26.160 to variables in the same declaration. 51 04:28.670 --> 04:32.940 Let's say that you want to change the width and declare a new variable named color. 52 04:34.090 --> 04:35.430 You can do so like this. 53 04:41.040 --> 04:43.980 This first assigns 50 to the width variable. 54 04:43.980 --> 04:51.300 So it changes its value to 50. And, here it declares a new variable named color with an initial value 55 04:51.300 --> 04:51.880 of red. 56 04:53.890 --> 04:56.730 But this is verbose and it's not simple. 57 04:56.860 --> 05:00.800 Let's do the same thing using a re-declaration. 58 05:00.890 --> 05:06.590 I'm going to comment out those first. Then, I'm going to use this redeclaration (statement) instead. 59 05:11.860 --> 05:16.320 This will change the width to 50 and at the same time, it will declare the color variable with 60 05:16.330 --> 05:25.390 an initial value of red. Looks better, right? As you'll learn later that redeclaration both a gift 61 05:25.390 --> 05:32.980 and it's also a curse. It's because sometimes shadowing the already declared variables an be a nightmare. 62 05:33.810 --> 05:37.620 Check out the link in the resources to see what shadowing means. 63 05:37.680 --> 05:39.470 I've created an example code for you there. 64 05:39.470 --> 05:44.390 I'll also talk about the shadowing in upcoming sections. 65 05:44.400 --> 05:51.870 Also, as you'll learn later that short declaration has very useful properties. For example, it can be used 66 05:51.900 --> 06:00.710 inside if and switch statements to create variables that belong to those statements only. 67 06:00.910 --> 06:08.220 That's all! Well done. You've learned everything about declaring variables in Go. 68 06:08.540 --> 06:09.710 See you in the next lecture.