WEBVTT 0 00:01.100 --> 00:08.050 Welcome! This lecture is about building a program that can split the given URL path into its parts. 1 00:08.840 --> 00:10.050 Along the long road, 2 00:10.090 --> 00:16.200 you'll also learn about three things: 1- You'll learn a new package from Go Standard Library (stdlib) called "path". 3 00:17.050 --> 00:22.900 2- You'll learn how to use multiple-result returning expressions in multiple assignments. 4 00:23.540 --> 00:27.990 3- Lastly, you'll learn about discarding one of those values (blank-identifier). 5 00:28.040 --> 00:29.480 It's time to get started. 6 00:29.630 --> 00:34.450 As I said, in this project, I'm going to use a function from the Go Standard Library. 7 00:35.350 --> 00:43.910 Here it is! Its name is "path". The path package provides utility functions for working with URL path 8 00:43.980 --> 00:45.140 string (values). 9 00:45.400 --> 00:52.890 For now, I'm only interested in its "Split" function. It's because I want to separate a URL to 10 00:52.980 --> 00:56.030 directory and file paths. 11 00:56.040 --> 00:58.090 Let's take a look at this function in detail now. 12 01:03.730 --> 01:08.480 Here it is! This function takes "path" as a string. 13 01:08.790 --> 01:16.650 So it has one input parameter: "path", and it splits the given path somehow, and returns a directory path 14 01:16.740 --> 01:24.710 and a file name as "string (value)s". So it has two result values: "dir" and "file". 15 01:24.720 --> 01:32.220 This means that if you call this function it will return two values. Actually, by learning about variables, 16 01:32.240 --> 01:38.550 you've also learned how to declare input parameters and result values for functions. 17 01:38.550 --> 01:44.070 Remember that, (when declaring a variable), a type is used (only) once, all variables get the same type. 18 01:44.430 --> 01:50.870 So this also true here. Both the dir and the file result values are strings here. 19 01:50.870 --> 01:56.580 It's just like a parallel variable declaration that you've seen previously. 20 01:57.410 --> 02:07.600 The type of the dir result value is a string and the type of file result value is also a string. 21 02:07.600 --> 02:14.280 Let's say that I have called this function with this path value. Then this function will return the 22 02:14.290 --> 02:22.820 directory-path like this, and it will return the filename name like this. As you can see, it splits the given 23 02:22.820 --> 02:30.710 path and returns it as two string values. The first one is the directory and the second one is the 24 02:30.710 --> 02:31.500 file. 25 02:31.880 --> 02:32.270 OK. 26 02:32.270 --> 02:34.260 Now you know how this function works. 27 02:34.310 --> 02:38.620 Let's use it in a code example. 28 02:38.780 --> 02:44.100 In this program, I'm going to split a given URL path to its directory and file name parts. 29 02:44.110 --> 02:44.570 To do that, 30 02:44.660 --> 02:47.570 I'm going to use a multiple assignment statement. 31 02:47.690 --> 02:50.620 First let's declare two new string variables. 32 02:51.670 --> 03:01.070 "dir" will store the directory path, and "file" will store the filename. Now, let's use these variables in a multiple 33 03:01.070 --> 03:02.840 assignment like this. 34 03:03.630 --> 03:08.720 Ok, now, I'm going to call the "path.Split" function with a path value like this. 35 03:08.870 --> 03:18.010 "css/main.cs". "css" is a directory and "main.css" is the file inside that 36 03:18.010 --> 03:18.850 directory. 37 03:20.050 --> 03:24.390 As you know, the Split function returns to string values. 38 03:24.400 --> 03:30.780 Here, they are being saved into the "dir" and the "file" variables. In this assignment, 39 03:30.850 --> 03:36.510 the number of variables and the number of values that will be returned from "Split" function are the same. 40 03:37.590 --> 03:42.350 As you can see, there are two variables on the left-hand side of the assignment statement: 41 03:42.360 --> 03:47.710 "dir" and "file". And, the path.Split function returns two values. 42 03:47.800 --> 03:52.410 So the numbers match. OK, let's print these variables 43 03:52.410 --> 03:52.740 now. 44 03:56.640 --> 04:00.270 Good! It prints the directory and file names. 45 04:00.450 --> 04:05.370 By the way, you can also discard some of the values if you don't want to use them, like this. 46 04:08.180 --> 04:13.850 Let's say that you only want to learn about the filename part of this URL path, but, 47 04:13.880 --> 04:16.950 you don't care about the directory name. To do that, 48 04:17.000 --> 04:22.060 you can use a blank identifier in place of the directory variable. 49 04:22.060 --> 04:27.610 Now, the first returned value from the Split function will be discarded by this blank identifier. 50 04:31.660 --> 04:32.500 As you can see, 51 04:32.500 --> 04:38.330 this time, it only prints the filename. It's because there is no longer a directory variable anymore. 52 04:38.590 --> 04:42.590 The blank identifier has discarded it. Allright. 53 04:42.600 --> 04:45.840 Now, take a look at the last example. 54 04:45.920 --> 04:50.680 Actually, you could have used a short declaration instead of an assignment. 55 04:50.690 --> 04:57.620 So here, I have changed the assignment into a short declaration statement right now. 56 05:00.320 --> 05:06.210 As you can see, the result is the same, but I think that this code is more concise than the previous version, right? 57 05:06.350 --> 05:10.950 So you have learned about the path package and its Split function. 58 05:11.290 --> 05:18.020 You have also learned that when an expression returns multiple values, you can use it either in an assignment 59 05:18.140 --> 05:20.990 or in a short declaration statement. 60 05:21.080 --> 05:24.670 That's all for now! 61 05:24.730 --> 05:30.700 Alright, congrats! You have learned the basics of assignment and that's all for now. See you in the next lecture.