1 00:00:00,780 --> 00:00:03,370 And go, as in most programming languages. 2 00:00:03,370 --> 00:00:06,160 A variable's a name that refers to a value. 3 00:00:06,160 --> 00:00:09,980 In go, you need to declare a variables before you can use them. 4 00:00:09,980 --> 00:00:12,670 So here's a simple variable declaration here, and 5 00:00:12,670 --> 00:00:15,360 then we assign to it down here on this slide. 6 00:00:15,360 --> 00:00:17,690 Each variable you declare needs to have a type. 7 00:00:17,690 --> 00:00:21,122 So here we say that this variable a will have a type of int, and 8 00:00:21,122 --> 00:00:24,530 then we can only assign integers to that variable. 9 00:00:24,530 --> 00:00:27,960 You can declare multiple variables of the same type at once. 10 00:00:27,960 --> 00:00:30,980 So here, we declare integer variables named b and c, and 11 00:00:30,980 --> 00:00:34,230 here we assign values to both of those variables. 12 00:00:34,230 --> 00:00:37,440 You don't have to declare the type of a variable if it can be inferred 13 00:00:37,440 --> 00:00:39,410 from its initial assignment, however. 14 00:00:39,410 --> 00:00:44,430 So if you assign an integer value 5 to the variable d here on the line 15 00:00:44,430 --> 00:00:48,820 where you declare it, you don't need to declare the type of the d variable. 16 00:00:48,820 --> 00:00:51,701 It will be just inferred that since this is an integer value, 17 00:00:51,701 --> 00:00:53,178 the type odd is going to be int. 18 00:00:53,178 --> 00:00:56,438 Go also offers short variable decorations, and 19 00:00:56,438 --> 00:01:01,500 this will be the most common form of variable decoration you will use. 20 00:01:01,500 --> 00:01:04,870 You use this syntax right here, a : followed by an = sign, 21 00:01:04,870 --> 00:01:08,630 with the initial value that you want to assign to the variable. 22 00:01:08,630 --> 00:01:12,202 That's the same as saying var e int = 6. 23 00:01:12,202 --> 00:01:17,711 Now if we run this sample, We'll get a compile error for 24 00:01:17,711 --> 00:01:22,408 all these samples saying, they've been declared and not used. 25 00:01:22,408 --> 00:01:25,440 Just like your required to use every package you importing go, 26 00:01:25,440 --> 00:01:28,500 you also have to use every variable you declare. 27 00:01:28,500 --> 00:01:32,860 An unused variable often indicates a bug, and go helps your development team spot 28 00:01:32,860 --> 00:01:36,140 these bugs quicker, by reporting unused variables. 29 00:01:36,140 --> 00:01:40,050 Since this is just a quick example, the way we'll use the variables is by updating 30 00:01:40,050 --> 00:01:41,690 the last line to print them all out. 31 00:01:41,690 --> 00:01:47,258 So we'll say format.print line a, 32 00:01:47,258 --> 00:01:49,960 b, c, d, and e. 33 00:01:49,960 --> 00:01:51,430 Save that and rerun it. 34 00:01:52,470 --> 00:01:55,330 And now that we're actually accessing all the variable names somewhere, 35 00:01:55,330 --> 00:01:58,225 the program will compile and run successfully. 36 00:01:58,225 --> 00:02:02,865 It's also possible to assign a multiple variables at the same time using commas. 37 00:02:02,865 --> 00:02:07,087 So here on this line, we declare a and b variables of type int, and here, 38 00:02:07,087 --> 00:02:09,868 we assign values to both a and b at the same time. 39 00:02:09,868 --> 00:02:14,157 1, 2 gets assigned to a, b. 40 00:02:14,157 --> 00:02:17,107 That is a becomes 1, and b becomes 2. 41 00:02:17,107 --> 00:02:20,897 Here, we assign initial values to the variable c and 42 00:02:20,897 --> 00:02:23,197 d right here on the declaration line. 43 00:02:23,197 --> 00:02:27,057 And here, we use a short variable declaration to assign initial values to 44 00:02:27,057 --> 00:02:30,890 the variables e and f, while simultaneously declaring them. 45 00:02:30,890 --> 00:02:33,053 And if we run this program, you see that it prints values for 46 00:02:33,053 --> 00:02:34,270 all the variables out down here. 47 00:02:34,270 --> 00:02:39,230 The same rules apply for the names of variables, constants, functions, 48 00:02:39,230 --> 00:02:41,090 and custom types. 49 00:02:41,090 --> 00:02:43,650 A variable name has to begin with a letter. 50 00:02:43,650 --> 00:02:47,610 So, if it begins with a number as it does down here, that's not valid. 51 00:02:47,610 --> 00:02:51,610 A variable name can be followed by any number of letters or numbers, however. 52 00:02:51,610 --> 00:02:56,900 So as long as a variable name begins with a letter, it can be followed by numbers. 53 00:02:56,900 --> 00:03:00,412 If the first letter is lowercase, the variable is unexported, and 54 00:03:00,412 --> 00:03:02,760 can only be used within the current package. 55 00:03:02,760 --> 00:03:06,110 The same is true for constants, functions, etc. 56 00:03:06,110 --> 00:03:09,840 If the first letter is uppercase, however, the variable is exported, and 57 00:03:09,840 --> 00:03:12,450 can be used outside the current package. 58 00:03:12,450 --> 00:03:16,080 You can only assign values of the declared type to a variable. 59 00:03:16,080 --> 00:03:20,709 So let's say that we have a variable named wholeNumber declared as an int, 60 00:03:20,709 --> 00:03:25,200 and a second variable named fractionalNumber declared as a float64. 61 00:03:25,200 --> 00:03:30,246 If we try to declare a new variable of an int type named wholeNumber2, 62 00:03:30,246 --> 00:03:33,923 and try to assign the fractionalNumber2 to it, or 63 00:03:33,923 --> 00:03:39,652 try to declare a new float64 variable and try to assign the wholeNumber to it, 64 00:03:39,652 --> 00:03:44,012 we'll get compiler errors, saying that we cannot use a type 65 00:03:44,012 --> 00:03:48,980 float64 value as an int value for assignment to a variable. 66 00:03:48,980 --> 00:03:53,240 And we can't use an int type for assignment to a float64 variable. 67 00:03:53,240 --> 00:03:56,650 We can assign the values if we do a conversion, however. 68 00:03:56,650 --> 00:04:00,910 So, if we were to type int and 69 00:04:00,910 --> 00:04:05,700 put fractional number in parentheses here, that would convert the fractional 70 00:04:05,700 --> 00:04:09,790 number value to an int before attempting to assign it to the int variable. 71 00:04:10,920 --> 00:04:15,760 Likewise down here, we can put the type float64 and 72 00:04:15,760 --> 00:04:18,100 in parenthesis put wholeNumber. 73 00:04:18,100 --> 00:04:22,810 That will convert a whole number to a float value before trying to assign 74 00:04:22,810 --> 00:04:25,230 it to a float64 variable. 75 00:04:25,230 --> 00:04:27,090 Let's save that, and try running this again. 76 00:04:28,220 --> 00:04:30,530 And this time, it's successful. 77 00:04:30,530 --> 00:04:34,040 So anytime you need to do a conversion, you write the type that you want to 78 00:04:34,040 --> 00:04:38,450 convert to, followed by the value that you want to convert in parentheses. 79 00:04:38,450 --> 00:04:42,075 You also need to convert both operands to the same type prior to do again 80 00:04:42,075 --> 00:04:44,410 a math operations or comparisons. 81 00:04:44,410 --> 00:04:48,820 So up here, we have an int and a float64 variable. 82 00:04:48,820 --> 00:04:50,770 And we try to add one to the other, 83 00:04:50,770 --> 00:04:54,680 as well as do a less than comparison between one and the other. 84 00:04:54,680 --> 00:04:56,966 And we can see that we get compile errors down here. 85 00:04:56,966 --> 00:05:00,959 If we try to add the fractionalNumber to the wholeNumber, 86 00:05:00,959 --> 00:05:04,485 we get mismatched types int and float64. 87 00:05:04,485 --> 00:05:09,005 And we get the same error, if we try to do a comparison between those two values. 88 00:05:09,005 --> 00:05:11,855 Again, we can get this working by doing a conversion first. 89 00:05:11,855 --> 00:05:14,655 So let's convert both of our int values to float64s. 90 00:05:16,410 --> 00:05:22,134 Float64 wholeNumber and we'll do a conversion here as well, 91 00:05:22,134 --> 00:05:24,680 float64, wholeNumber. 92 00:05:24,680 --> 00:05:29,510 Save that, rerun it, and this time, it works. 93 00:05:29,510 --> 00:05:33,850 A variable scope that is the section of your programming which it's visible, 94 00:05:33,850 --> 00:05:36,720 is limited to the block in which it's defined. 95 00:05:36,720 --> 00:05:40,140 Blocks are a chunk of codes surrounded by curly braces. 96 00:05:40,140 --> 00:05:43,300 They're usually associated with functions, if statements, and 97 00:05:43,300 --> 00:05:45,440 loops, all of which we'll see later. 98 00:05:45,440 --> 00:05:48,260 But it's possible to use blocks all by themselves. 99 00:05:48,260 --> 00:05:53,000 Even though it's not surrounded by curly braces, each package is an implicit block. 100 00:05:53,000 --> 00:05:56,560 Variables defined at package level are accessible from every block 101 00:05:56,560 --> 00:05:58,410 nested within that package. 102 00:05:58,410 --> 00:06:01,840 Most importantly, that includes the bodies of functions. 103 00:06:01,840 --> 00:06:05,890 So if we would define a variable named a appear at package level, 104 00:06:05,890 --> 00:06:09,220 that will be visible everywhere here within mean and 105 00:06:09,220 --> 00:06:13,350 within any other function that we declare within the main package. 106 00:06:14,712 --> 00:06:19,271 Now this b variable, however, is defined within the main function, 107 00:06:19,271 --> 00:06:23,070 which you'll notice, its body is surrounded by a block. 108 00:06:23,070 --> 00:06:27,174 So that b variable is only visible here within the main function, and 109 00:06:27,174 --> 00:06:31,230 within any other blocks that are nested within that main function. 110 00:06:32,300 --> 00:06:33,790 Same thing with this block here. 111 00:06:33,790 --> 00:06:36,550 We've defined the c variable inside it, so 112 00:06:36,550 --> 00:06:39,430 that's only visible within the scope of this block. 113 00:06:40,480 --> 00:06:43,840 And this d variable is visible only within this innermost block. 114 00:06:44,935 --> 00:06:49,445 So you can see that we make a reference to the values or 115 00:06:49,445 --> 00:06:52,965 to the variables a, b, c, and d, here on several lines. 116 00:06:52,965 --> 00:06:57,573 However, the only one that doesn't have any issues is line 13 here. 117 00:06:57,573 --> 00:07:02,826 That's because the c, b, and a variables were all declared before this block, 118 00:07:02,826 --> 00:07:06,060 and are therefore still visible within. 119 00:07:06,060 --> 00:07:09,600 However the d variable was declared inside this block, and 120 00:07:09,600 --> 00:07:14,040 therefore, its scope lasts only as long as this block is still running. 121 00:07:14,040 --> 00:07:17,420 So when we try to reference the d variable down here on this line, 122 00:07:17,420 --> 00:07:22,560 we get an error at compile time saying that the d variable is undefined. 123 00:07:22,560 --> 00:07:26,340 Same thing with line 17, we get an error saying that both the c and 124 00:07:26,340 --> 00:07:28,690 the d variables are undefined down there. 125 00:07:28,690 --> 00:07:33,380 Because the d variable was defined up here in this block, and is therefore, 126 00:07:33,380 --> 00:07:38,790 its scope only last for that block and the c variable was defined in this block, 127 00:07:38,790 --> 00:07:41,730 which is also out of scope by the time we get down to this line. 128 00:07:42,860 --> 00:07:46,610 So, there is a quick overview of declaring and using variables and go. 129 00:07:46,610 --> 00:07:48,850 Don't worry if you don't remember all those details, 130 00:07:48,850 --> 00:07:51,410 we'll be recapping as needed in later videos.