WEBVTT 0 00:01.080 --> 00:07.510 Welcome, in this lecture I'm going to talk about the variable declaration syntax. 1 00:07.510 --> 00:14.560 For example to declare a variable you need to follow 2 00:14.560 --> 00:18.740 the rules of the variable declaration syntax of Go. 3 00:18.860 --> 00:23.210 Also you need to declare a variable before you can use it. 4 00:23.210 --> 00:28.310 So you can't just throw a value to an unknown variable that doesn't exist yet. 5 00:28.310 --> 00:32.650 This is important and necessary for the compile time safety. 6 00:32.720 --> 00:38.990 For example in a dynamic programming language you can just assign a value to a variable even before 7 00:39.350 --> 00:42.020 declaring it. It is easier to use 8 00:42.030 --> 00:46.020 but it may not be easy for you to maintain your program afterwards. 9 00:46.280 --> 00:48.360 Let's start with an example. 10 00:48.470 --> 00:54.740 Imagine that you have a customer who wants you to write a program that will record various measurements 11 00:54.740 --> 00:58.260 from vehicles. In this imaginary scenario 12 00:58.300 --> 01:02.390 your program will be installed in two millions of vehicles. 13 01:02.830 --> 01:04.140 Oh my goodness. 14 01:04.600 --> 01:06.910 OK, then you go and start programming. 15 01:06.990 --> 01:11.560 Here is the usual package main and main function as an entry point to our program. 16 01:12.690 --> 01:17.690 I'm going to declare a variable for storing the current speed of a car, like this. 17 01:18.610 --> 01:22.430 Let's zoom a little bit into this declaration to understand it better. 18 01:22.430 --> 01:26.750 You might have already guessed that this is the variable declaration syntax in Go. 19 01:27.350 --> 01:30.650 Now let's take a look at the parts of this syntax. 20 01:30.650 --> 01:32.250 This "var" keyword means 21 01:32.290 --> 01:38.780 variable. It simply tells Go that let's declare a variable. 22 01:38.790 --> 01:45.380 So you always have to type this keyword first. After the "var" keyword, you need to give a unique name to your variable. 23 01:46.190 --> 01:49.290 Here the speed is the name of this variable. 24 01:49.370 --> 01:55.350 More formally, names are also called identifiers. A name or an identifier helps 25 01:55.470 --> 01:59.540 you and Go to understand each variable you are referring to. 26 01:59.540 --> 02:03.430 And as I said before a variable gives a name to a value, 27 02:03.710 --> 02:05.420 so you can reuse it later. 28 02:06.300 --> 02:12.130 Naming your variable is such an important concept that there is a saying that goes like this. 29 02:12.770 --> 02:16.620 There are only two hard things in computer science. 30 02:16.910 --> 02:20.410 Cache invalidation and naming things. 31 02:20.690 --> 02:21.900 And I agree with that. 32 02:22.190 --> 02:26.240 I'll talk about a few tips about selecting good names later. 33 02:26.600 --> 02:29.600 Let's take a look at a few rules about names. 34 02:29.600 --> 02:36.200 By the way, keep in mind that these naming rules are not only for variables, they also apply to other 35 02:36.200 --> 02:40.710 names such as function names, package names and so on. OK. 36 02:40.710 --> 02:46.800 The first rule is that a name should always start with a letter or an underscore character. 37 02:46.800 --> 02:53.070 This is because, as you've seen before, when the first letter of a name is an uppercase letter that name 38 02:53.070 --> 02:57.120 becomes exported so that other packages can use it. 39 02:57.120 --> 03:00.870 That's one of the reasons why a name should start with a letter. 40 03:00.870 --> 03:03.830 OK, let's take a look at a few more examples. 41 03:03.840 --> 03:07.790 A name can contain uppercase letters in this example, 42 03:07.790 --> 03:12.460 this SpeeD name is exported since its first letter is an uppercase letter. 43 03:12.570 --> 03:15.410 It also ends an upper case letter. 44 03:15.450 --> 03:18.050 However I use it here as an example. 45 03:18.060 --> 03:24.620 Normally we don't name variables like this. As an exception a name can also start with an underscore 46 03:24.630 --> 03:25.420 character. 47 03:26.010 --> 03:33.030 And lastly a name can have Unicode letters. By the way, this word means speed in Japanese. 48 03:33.030 --> 03:38.000 I hope that is true. This works because Go can understand Unicode letters. 49 03:38.400 --> 03:41.110 Now let's take a look at what you can't do. 50 03:41.310 --> 03:48.270 For example a name cannot start with a number like this, or a name can't start with a punctuation character 51 03:48.270 --> 03:49.470 like this. 52 03:49.470 --> 03:57.360 Also a name can't contain a punctuation character inside or it can't end with it. It's because punctuation 53 03:57.360 --> 03:59.960 characters have special meanings in Go 54 04:00.480 --> 04:04.630 and it is easier for Go compilers to parse the names. 55 04:04.980 --> 04:10.110 And lastly, a name can't only contain reserved Go keywords. 56 04:10.440 --> 04:16.760 For example if Go was allowing you to do that, you'll never be able to declare a variable since the var 57 04:16.990 --> 04:23.950 keyword declare a new variable. So Go doesn't allow you to override its own keywords. As you've seen 58 04:24.090 --> 04:32.310 these rules are there to prevent ambiguitys. They help Go compiler to understand what you mean and they 59 04:32.310 --> 04:35.580 make the compiler's job easier. 60 04:35.670 --> 04:38.430 OK, that's all for the naming rules. 61 04:38.420 --> 04:49.570 Let's continue. Here int is a type of this variable. It is short for integer. In Go every variable and value has 62 04:49.570 --> 04:54.370 a type. It's because Go is a strongly typed programming language. 63 04:54.370 --> 04:57.010 It's not like Python or Javascript. 64 04:57.010 --> 05:04.480 It's more like C, Java, and C-Sharp. This helps the compiler to find bugs in your program even before you run 65 05:04.480 --> 05:04.860 it. 66 05:05.890 --> 05:12.880 A variable's type determines what type of values you can store in the variable, and where you can use that 67 05:12.880 --> 05:14.030 variable. 68 05:14.110 --> 05:17.210 For example this is an integer variable, right? 69 05:17.440 --> 05:24.160 So you can only store an integer value in this variable, and you can only use it with other integer 70 05:24.160 --> 05:26.720 values or variables. 71 05:26.740 --> 05:30.600 So as I said in the introduction, once you declare a type for a variable 72 05:30.670 --> 05:32.530 you can't change it afterwards. 73 05:32.580 --> 05:33.570 It's static. 74 05:33.970 --> 05:41.760 If you could do so then there wouldn't be this strong type safety of Go. That would invite bugs. 75 05:42.380 --> 05:47.740 OK, so now you know what the variable declaration syntax looks like in Go. 76 05:48.120 --> 05:50.850 Let's get back to the example program. 77 05:50.910 --> 05:56.970 Now let's print this variable to the console. Let me type Println. 78 05:56.990 --> 05:57.950 All right, ready. 79 05:58.170 --> 05:59.050 Let's run it now. 80 06:00.910 --> 06:02.180 It prints 0. 81 06:02.440 --> 06:07.510 It's because a variable is initialized to a zero value when it's been declared 82 06:07.530 --> 06:09.080 without an initial value. 83 06:09.330 --> 06:11.940 I'll explain what a zero-value means later. 84 06:11.940 --> 06:19.230 Now let's take a look at what happens when you use a variable before you declare it. In Go the order 85 06:19.240 --> 06:24.640 statements are very very important. Since Go runs the code from top to bottom. 86 06:24.730 --> 06:31.200 Here, I have moved the variable declaration after the Println call. Can you guess what would happen now? 87 06:31.200 --> 06:34.360 OK let's run it. 88 06:35.930 --> 06:44.770 This program cannot even compile, let alone run! The error message says that the speed variable hasn't been declared. 89 06:44.770 --> 06:47.010 This means that it doesn't exist yet. 90 06:47.080 --> 06:49.260 So Println can't print it. 91 06:49.690 --> 06:54.630 If you're an experienced programmer this is not a surprise for you. 92 06:54.670 --> 07:01.510 However if you are coming from a language such as Javascript then you know the difference. If you don't 93 07:01.510 --> 07:03.540 know Javascript let me tell you that 94 07:03.620 --> 07:06.230 in Javascript you can do this. 95 07:06.340 --> 07:10.400 I mean you can use a variable even before you declare it. 96 07:10.500 --> 07:13.730 It even has a name: variable hosting. 97 07:13.750 --> 07:15.610 Check out the link in the resources. 98 07:15.640 --> 07:17.240 You will understand what I mean. 99 07:17.290 --> 07:20.140 I have prepared a program for you in Javascript. 100 07:20.490 --> 07:23.660 So in the end the speed variable looks like this. 101 07:24.700 --> 07:32.860 Its name is speed, it's static type is int, and Go has assigned it a value of zero automatically. And the type 102 07:32.860 --> 07:37.000 of its value is also int. So congrats, 103 07:37.000 --> 07:40.180 Now we know how to declare a variable. By the way 104 07:40.180 --> 07:46.760 I know that this was a lot of talk but don't worry you'll see more examples as you progress. 105 07:46.780 --> 07:48.570 So you'll understand everything 106 07:48.580 --> 07:52.630 naturally. However this also depends on you. 107 07:52.650 --> 07:59.910 You can always pause the videos and type the programs along with me, so you can try them yourself. 108 07:59.910 --> 08:02.360 It is the best way to understand Go. 109 08:02.670 --> 08:04.460 All right thank you for watching. 110 08:04.460 --> 08:05.550 See you in the next lecture.