WEBVTT 0 00:00.680 --> 00:07.090 Previously, you learned that you cannot assign a float64 value to an int variable. Fortunately, Go is 1 00:07.100 --> 00:13.190 not always that rigid. Actually, Go just wants you to be explicit on everything. 2 00:13.340 --> 00:19.100 That's why it doesn't let you assign a float64 value to an int value. Since it actually wants 3 00:19.100 --> 00:20.270 your approval. 4 00:20.530 --> 00:24.070 In the lecture, you're going to learn about the type conversion expression. 5 00:24.620 --> 00:30.620 Basically, a type conversion creates a new value by changing the type of the original value to another 6 00:30.620 --> 00:31.040 type. 7 00:31.910 --> 00:34.360 All right, let's see how it works. 8 00:38.810 --> 00:40.920 This is a type conversion expression. 9 00:41.600 --> 00:43.730 It looks like a function call, right? 10 00:43.930 --> 00:47.520 But, instead of a function name, it starts with a type name. 11 00:47.650 --> 00:56.380 So first, you type the name of the "type", and within the parenthesis, you provide it a value. This conversion 12 00:56.380 --> 01:03.640 expression changes the type of a given value to another type. As you know, a variable has a value and 13 01:03.640 --> 01:04.250 in Go, 14 01:04.250 --> 01:09.900 there are also function values, constant values, interface values, and the list goes on. 15 01:09.920 --> 01:16.130 So the conversion expression is not only for converting the type of variables. You can use it to convert 16 01:16.160 --> 01:20.420 almost any value to another type as well. Of course, 17 01:20.420 --> 01:27.110 to do that, the type of the value should be convertible to that type that you want to convert into. 18 01:27.130 --> 01:29.020 Anyway, let's take a look at an example now. 19 01:37.260 --> 01:37.670 OK. 20 01:37.690 --> 01:41.490 First, I'm going to show you what you can do. 21 01:41.520 --> 01:44.030 I'm going to assign a new value to the speed variable. 22 01:44.030 --> 01:51.260 I'm going to type "speed", and I'm going to multiply it with the force variable. 23 01:51.300 --> 01:57.810 So in the end, the result of this expression will be assigned to the speed variable. As you now know, these 24 01:57.810 --> 02:00.270 variables have different types. 25 02:00.600 --> 02:08.450 So the type of the speed variable is int, whereas the type of the force variable is float64. 26 02:08.450 --> 02:13.340 So when you run this program, there would be an error like this. 27 02:13.400 --> 02:20.410 It clearly tells you that the speed variable is an int but the force variable is a float64. As you've 28 02:20.410 --> 02:21.210 seen before, 29 02:21.260 --> 02:27.720 you cannot use different types of values together even though they both are numeric types. 30 02:27.770 --> 02:30.060 I have said enough about the problem. 31 02:30.260 --> 02:32.460 Let's talk about the solution now. 32 02:32.780 --> 02:36.780 Let's check it out. To solve the problem, 33 02:36.810 --> 02:40.100 I need the convert one of these variables. 34 02:40.200 --> 02:43.920 So, which one of these variables should I convert first? 35 02:45.270 --> 02:51.910 Should I convert the speed variable into float64, or should I convert the force variable into an int? 36 02:53.280 --> 02:57.740 For now, let's convert the force variable to an int type like this. 37 03:00.220 --> 03:07.210 Alright, now let's print out the result, and then let's run it to see what it does. OK. 38 03:07.230 --> 03:09.180 This time, it works. 39 03:09.180 --> 03:15.770 It works because inside the expression the force variable's value becomes an int on the fly. 40 03:17.010 --> 03:24.170 However, this result is not the result that I would like to see. Think about it. Multiplying 100 with 41 03:24.660 --> 03:30.100 2.5 is 250, right? It's not 200. 42 03:30.540 --> 03:33.250 So, can you guess why the result is wrong? 43 03:40.270 --> 03:46.780 Clearly, int(force) conversion trimmed the fractional part of the force variable's value. 44 03:47.050 --> 03:51.140 So it converts 2.5 to 2. 45 03:51.260 --> 03:54.590 That's because, an int value can't have a fractional part. 46 03:54.960 --> 04:02.400 So this is a destructive operation. Since the fractional part literally vaporizes. As you can see, 47 04:02.430 --> 04:09.120 the conversion can also change the value itself, not only its type. 48 04:09.330 --> 04:16.280 Anyway, let's also print the force variable, before and after the type conversion. 49 04:16.320 --> 04:24.150 Let's run it now. As you can see, the force variable is still the same, but the converted value is 50 04:24.150 --> 04:26.350 different, right? 51 04:26.490 --> 04:33.470 This means that the conversion creates a new value but it doesn't change the actual force variable. 52 04:33.960 --> 04:39.810 So the conversion doesn't change the original value. 53 04:39.810 --> 04:44.800 Now let's talk about how to solve the previous information destruction problem. 54 04:44.910 --> 04:51.480 So instead of converting the force variable, this time, let's first convert the speed variable. 55 04:51.520 --> 04:53.770 However, this is not enough. 56 04:53.840 --> 04:57.290 It's because, the speed variable is not a float64, 57 04:57.430 --> 05:02.300 it's an int. If I would run this, Go compiler will terminate (end/exits). 58 05:02.690 --> 05:04.850 Let me show you the same code in the editor. 59 05:17.130 --> 05:23.770 Let's take a look at the error here. Go (compiler) says that I cannot assign a float64 value to an int variable. 60 05:23.830 --> 05:28.740 It's because the type of this expression is 61 05:28.750 --> 05:37.740 float64, not int. Since the type conversion has to change the type of the speed to float64 here. 62 05:38.580 --> 05:45.480 However, the speed variable here is an int. So I can't assign a float64 value to the speed variable. 63 05:45.480 --> 05:47.290 To do that, 64 05:47.310 --> 05:57.600 I also need to convert this whole expression into an int. Let's get back and do that. 65 05:57.640 --> 06:01.200 Now, let's run it again. 66 06:01.350 --> 06:03.100 Now, the result is correct. 67 06:03.330 --> 06:11.790 100 multiplied by 2.5 equals 250, right? As you can see, the order 68 06:11.790 --> 06:17.570 of conversions and which value to convert first are very important for the correct results. 69 06:18.460 --> 06:26.060 Let me explain why this works. In the expression, it first converts the value of the speed variable. 70 06:26.780 --> 06:31.890 So it becomes 100.0 which is float64 value. 71 06:32.330 --> 06:35.840 Then it multiplies the result with the force variable. 72 06:35.840 --> 06:46.110 This means it multiplies two float values. So it multiplies 100.0 by 2.5. So the result 73 06:46.110 --> 06:49.320 becomes 250.0 74 06:49.620 --> 06:56.820 which is another float value. And, in the end, it converts the result value of 250.0 to an int 75 06:56.820 --> 06:57.500 value. 76 06:58.050 --> 07:04.970 So the result becomes 250 which is an int value. Alright. 77 07:04.990 --> 07:06.490 That's all for now. 78 07:06.540 --> 07:09.310 In the next lecture, I'm going to show you another example.