WEBVTT 0 00:00.800 --> 00:06.150 Welcome to this coding lecture! In this lecture, I'm going to show you a few examples about Printf. 1 00:07.150 --> 00:08.960 OK and let's get started. 2 00:08.960 --> 00:13.060 Now I'm going to show you how you can print the types of variables using Printf. 3 00:13.300 --> 00:19.150 By the way, you're not limited to variables only. Printf can also print the type of any value whether it's 4 00:19.150 --> 00:20.650 a variable or not. 5 00:21.700 --> 00:27.650 First, let me show you an example here. Then, I'm going to open my code editor for further examples. 6 00:27.820 --> 00:31.840 OK, let's declare a few variables with different types: var speed int 7 00:31.840 --> 00:38.360 var heat float64, var off bool, var brand as a string variable. 8 00:38.460 --> 00:43.160 Let's print the type of the speed variable using printf like this. 9 00:44.320 --> 00:52.300 So to print the type of a value, I'm using "%T" verb here. It means: "type". 10 00:52.390 --> 00:57.490 Let's run it now. As you can see, it prints int. 11 00:57.570 --> 01:00.720 Let's take a look at the declaration of the speed variable above. 12 01:01.110 --> 01:04.760 You can see that Go prints its type as int which is correct. 13 01:04.770 --> 01:05.270 Right. 14 01:06.670 --> 01:11.270 Now let's print the type of the heat variable using printf again like this. 15 01:11.270 --> 01:15.450 Let's see how it prints to the console. It says float64. 16 01:15.480 --> 01:16.100 That's right. 17 01:16.990 --> 01:24.430 Now let's print the type of the off variable. It prints bool this time. 18 01:24.460 --> 01:32.250 Let's print the type of the brand variable. Pause the video and think about what it would print. 19 01:32.270 --> 01:33.470 Here is the answer. 20 01:33.590 --> 01:35.900 It prints string as expected. 21 01:35.900 --> 01:36.360 Alright. 22 01:36.410 --> 01:38.100 That's all for this example. 23 01:39.030 --> 01:47.620 Now let's take a look at more examples in the coding editor. 24 01:47.630 --> 01:51.700 Now I'm going to create a few variables using a multiple declaration statement. 25 01:51.980 --> 01:54.740 First, I'm going to declare a new variable named planet. 26 01:54.770 --> 01:56.900 with a string value like this. 27 01:58.560 --> 02:01.740 Let's print it using printf. 28 02:01.780 --> 02:07.200 I'm going to type planet. As you can see, you can type anything inside the formatting text. 29 02:07.630 --> 02:13.960 Now I'm going to type a new verb that you didn't see before. This "%v" verb is like a swiss-army knife 30 02:14.200 --> 02:14.900 for Printf. 31 02:14.920 --> 02:17.100 It can print any value. 32 02:17.470 --> 02:23.420 If you need to remember one thing about Printf, then this is it. OK, now I'm going to pass the planet variable 33 02:23.420 --> 02:24.660 to Printf. 34 02:24.990 --> 02:26.440 OK I'm going to run this program. 35 02:29.390 --> 02:35.950 As you can see, Printf prints the formatter by replacing the v verb with the planet variable ('s value). 36 02:35.990 --> 02:42.410 This is what I mean by saying that the formatter is like a template. Also notice that it didn't 37 02:42.410 --> 02:43.940 print a new line. 38 02:43.990 --> 02:46.700 It's because I didn't tell it to do so. 39 02:46.790 --> 02:50.310 Let me add a new line escape sequence to the formatter text. 40 02:50.350 --> 02:53.850 OK, I'm just going to add \n inside here. 41 02:54.880 --> 02:56.800 Alright, let's try it again. 42 02:56.830 --> 03:03.010 Did you notice it? Compare the last line with the previous one. This time in the last line, it prints 43 03:03.070 --> 03:03.780 a new line. 44 03:03.870 --> 03:04.610 Right. 45 03:07.070 --> 03:10.120 Now let me declare a few more variables. 46 03:10.140 --> 03:12.790 Here's the distance variable. 47 03:12.870 --> 03:16.270 It will store the distance to the planet Venus. 48 03:16.500 --> 03:19.180 On average Venus is 49 03:19.240 --> 03:23.100 261 millions of kilometers away from us. 50 03:23.160 --> 03:24.670 So let's initialize it. 51 03:24.680 --> 03:26.850 261 52 03:26.880 --> 03:34.140 Now I'm going to declare another variable named orbital, like this. Venus orbits around the sun about 53 03:34.140 --> 03:35.790 225 days. 54 03:37.110 --> 03:43.960 Now, I'm going to declare the last variable: hasLife. As far as we know there is no life on Venus, right? 55 03:43.960 --> 03:51.410 So I will just type false. OK let me print those variables using printf. 56 03:51.460 --> 03:59.790 I'm going to copy this line here, three times, using cmd+shift+d (ctrl+shift+d on Windows). Now I'm going to change this planet 57 03:59.790 --> 04:02.320 words here by using command+d again. 58 04:03.520 --> 04:04.270 Alright. 59 04:04.490 --> 04:10.430 Now I'm going to type distance here, and I'm going to pass the distance variable to printf here. 60 04:11.020 --> 04:14.550 Let me do the same thing for the orbital variable here. 61 04:22.650 --> 04:24.030 OK. 62 04:24.070 --> 04:24.480 Alright. 63 04:24.700 --> 04:26.710 Now I'm going to use two verbs. 64 04:26.770 --> 04:27.520 I'm going to print, 65 04:27.520 --> 04:29.690 "does Venus have life?" 66 04:30.470 --> 04:31.760 False. 67 04:31.860 --> 04:33.890 First I'm going to type: does. 68 04:34.250 --> 04:35.950 Then I'll put a verb here. 69 04:43.150 --> 04:45.990 So I'm going to pass the planet variable able to printf. 70 04:45.990 --> 04:52.330 This was for the first verb. It'll be replaced with Venus because the planet variable's value is 71 04:52.330 --> 04:59.060 Venus. And, lastly, I'm going to pass the hasLife variable to printf for the second verb, here. 72 04:59.420 --> 05:05.010 This verb will be replaced with false because the hasLife variable's value is false. 73 05:05.120 --> 05:10.380 As I said previously, you can add as many verbs and values to printf as you want. 74 05:10.430 --> 05:16.010 The rule is that there should be only one formatting text and the number of verbs and the number of 75 05:16.010 --> 05:18.130 values should match. 76 05:18.350 --> 05:20.640 OK let me fix this line a little bit. 77 05:21.660 --> 05:25.920 As long as you end it with a comma you can type it like this. 78 05:26.550 --> 05:31.430 OK let's try this now. Planet Venus 79 05:31.440 --> 05:37.010 Distance is 261 Orbital is 224 80 05:37.020 --> 05:39.050 All right everything looks right. 81 05:39.130 --> 05:46.640 Let's get back. As I said, the number of verbs and the number of values should match. 82 05:46.670 --> 05:49.370 Actually, there is an exception to that rule. 83 05:49.430 --> 05:54.460 It's called argument indexing. By using the argument indexing, 84 05:54.560 --> 06:01.460 you can refer to the passed values by their position. Let me show you. I'm just going to type Printf. 85 06:01.480 --> 06:08.430 %v is %v away with a newline character. 86 06:08.690 --> 06:12.490 Ok now I'm going to pass two variables: planet and distance. 87 06:12.530 --> 06:14.070 All right so far so good. 88 06:14.290 --> 06:16.040 This is what you've seen so far. 89 06:16.430 --> 06:17.660 OK let's run it 90 06:20.650 --> 06:23.300 It prints an expected message, of course. 91 06:23.300 --> 06:33.460 All right. Now, without changing the arguments, I'm going to change the formatting text like this. 92 06:33.470 --> 06:38.640 This is an argument index, and v here is just the usual v verb 93 06:38.690 --> 06:41.820 as you know. So it's typed like this. 94 06:42.800 --> 06:47.920 A percent sign and an index, then a formatting verb. 95 06:48.160 --> 06:55.630 So two hear tell Printf: "Get me the second argument". And, Printf replaces this whole verb with the 96 06:55.630 --> 06:57.720 distance variable. 97 06:57.720 --> 07:04.230 This is because if you exclude the formatting text, the distance here is the second argument to the printf. 98 07:04.350 --> 07:08.560 So it will replace this verb with the distance. 99 07:08.880 --> 07:15.820 And similarly, printf will also replace %[1]v here using the planet variable. 100 07:15.840 --> 07:19.450 This is because the planet variable's index position is one. 101 07:19.680 --> 07:22.160 It is the first argument to the Printf. 102 07:22.590 --> 07:25.620 So in Printf, the indexes start at 1. 103 07:25.650 --> 07:26.220 All right. 104 07:26.220 --> 07:29.490 Let's run this, as you can see, 105 07:29.560 --> 07:33.170 now the printf prints the values multiple times. 106 07:33.170 --> 07:36.150 Even I've passed two variables. 107 07:36.170 --> 07:37.820 OK, just bookmark this. 108 07:37.840 --> 07:41.260 Then you can return back to it later whenever you want. 109 07:41.260 --> 07:45.600 OK I'm going to finishing here. So I'm going to continue in the next lecture. 110 07:45.690 --> 07:47.480 OK see you, bye bye.