WEBVTT 0 00:00.210 --> 00:02.450 Welcome back again, let's continue. 1 00:02.490 --> 00:08.960 Alright. Now I'm going to show you how Printf provides type safety using verbs. I'm going to change 2 00:09.020 --> 00:10.540 the verbs here. 3 00:11.360 --> 00:17.260 Let's change "%v" to "s", for example. As you know, the planet variable is a string. 4 00:17.280 --> 00:22.210 So I have used "%s" here. "%s" means string. 5 00:22.500 --> 00:25.030 It should only be used with string values. 6 00:25.200 --> 00:29.060 Now let's also change "%v" here to %d. 7 00:29.120 --> 00:29.960 Now let's also change "%v" here to %d. 8 00:30.010 --> 00:33.280 Because distance is an integer. 9 00:33.290 --> 00:34.590 "%d" means decimal. 10 00:34.630 --> 00:44.010 Now, let's change "%v" here to "%f". It's because the orbital variable is a float. 11 00:44.300 --> 00:46.340 So, "%f" means float. 12 00:47.480 --> 00:53.210 The last two. Let's change %v here to %s. 13 00:53.340 --> 00:58.250 And lastly, let's change %v here to %t. 14 00:58.290 --> 01:00.980 This is because the hasLife variable is a bool. 15 01:01.420 --> 01:02.430 %t verb prints true or false. 16 01:02.430 --> 01:03.520 %t verb prints true or false 17 01:03.540 --> 01:07.200 depending on the value of the variable. 18 01:07.220 --> 01:10.270 Now all the printfs here are type-safe. 19 01:10.370 --> 01:11.600 I'll show you what I mean in a minute. 20 01:11.600 --> 01:19.040 Actually, when you use the %v verb, printf replaces %v verb with a typed verb under 21 01:19.040 --> 01:19.720 the hood. 22 01:20.090 --> 01:23.630 So printf is doing what I've done here behind the scenes. 23 01:23.720 --> 01:29.740 You might ask: "Why should I use these new verbs rather than just using the swiss-knife %v verb?" 24 01:30.090 --> 01:31.160 Right. 25 01:31.490 --> 01:32.920 It's a good question. 26 01:33.140 --> 01:36.740 However, you need to wait a little bit to get an answer for this. 27 01:36.830 --> 01:41.740 After this example, I'll explain it. No worries. 28 01:41.780 --> 01:50.020 First, run this one. As you can see, the result is almost the same as before, only the orbital here has more 29 01:50.020 --> 01:52.160 precision than the previous one. 30 01:52.210 --> 01:54.350 Precision means how many decimals there are. 31 01:54.370 --> 01:59.500 Let me show you what I mean by the type-safety. 32 01:59.500 --> 02:01.490 I'm going to change this %s here to 33 02:01.510 --> 02:07.230 %d. As you know, %d should only be used with an integer value. 34 02:07.230 --> 02:08.570 Right. 35 02:08.670 --> 02:10.570 However, here planet (variable) is a string. 36 02:10.720 --> 02:16.130 Let's see what happens. As you can see, Go warns me with a yellow underline. 37 02:16.200 --> 02:18.050 Go says that planet is a string. 38 02:18.060 --> 02:20.130 But you used a %d verb. 39 02:20.390 --> 02:23.950 %d verb is only for integers you smart one. 40 02:23.950 --> 02:32.910 OK let's change the others. Now it says that distance is an int but you used a verb for float values. 41 02:36.380 --> 02:45.860 And here it says that orbital is a float but you used a verb for string values. 42 02:45.950 --> 02:49.220 And here again it says that the planet is a string. 43 02:49.320 --> 02:51.980 But you used a verb for integer values. 44 02:55.900 --> 02:59.120 And, lastly, here it says that life is a boolean. 45 02:59.220 --> 03:01.850 But you used a verb for integer values. 46 03:02.220 --> 03:04.090 OK let's quickly fix those now. 47 03:04.260 --> 03:11.210 So this is what I mean by the type safety. Go compares the type of a value and its corresponding verb. 48 03:11.490 --> 03:18.570 If their type doesn't match then it warns you. Sometimes, type checking prevents unintentional errors. 49 03:18.690 --> 03:23.780 So always try to use the typesafe verbs instead of the swiss-knife %v verb. 50 03:23.970 --> 03:24.480 All right. 51 03:24.570 --> 03:26.510 This is my recommendation. 52 03:26.510 --> 03:27.020 All right. 53 03:27.210 --> 03:29.730 Now let's take a look at the last example here. 54 03:29.770 --> 03:34.110 I'm going to show you how to change the printed precision of a float number. 55 03:34.290 --> 03:36.180 You'll see what I mean in a second. 56 03:36.540 --> 03:40.630 OK let's first print the orbital as usual using the f verb. 57 03:45.050 --> 03:47.110 Now it prints as the previous one, right. 58 03:48.470 --> 03:53.750 Let's get back. Let's add a new line here to make it more readable in the console. 59 03:54.600 --> 03:56.490 OK let me copy this one here. 60 03:57.420 --> 04:03.000 Now I'm going to change the precision of this verb to zero. 61 04:03.070 --> 04:06.450 It prints 225 days. Much more readable, 62 04:06.450 --> 04:06.950 right. 63 04:07.040 --> 04:09.470 But it has lost its precision. 64 04:09.790 --> 04:16.010 This is what precision means. Instead of printing 224.701, 65 04:16.040 --> 04:20.320 as like here, it prints 225 days as in here. 66 04:20.320 --> 04:28.780 OK let's get back. Let's print it with one precision this time. 67 04:28.850 --> 04:33.650 Let's try. The difference is clear. Whenever you increase the precision, it becomes much more precise. 68 04:33.680 --> 04:36.890 And at the same time, it becomes much more unreadable. 69 04:36.890 --> 04:40.060 Let me add a bunch of these with increasing precision. 70 04:45.720 --> 04:46.010 All right. 71 04:46.020 --> 04:51.200 Let's try it. As you can see, the precision increases on each line. 72 04:51.200 --> 04:56.470 This means that some verbs have special properties like the f verb. In this course, 73 04:56.510 --> 04:58.370 you're going to learn about them as you progress. 74 04:58.370 --> 05:00.410 No worries. 75 05:00.410 --> 05:01.000 All right. 76 05:01.250 --> 05:01.870 Good job. 77 05:02.090 --> 05:05.820 Please work on the exercises if you want to really learn about printf. 78 05:06.020 --> 05:07.630 Thank you for watching so far. 79 05:07.630 --> 05:09.260 See in the next lecture. Bye!