Welcome! This lecture is about building a program that can split the given URL path into its parts.
Along the long road,
you'll also learn about three things: 1- You'll learn a new package from Go Standard Library (stdlib) called "path".
2- You'll learn how to use multiple-result returning expressions in multiple assignments.
3- Lastly, you'll learn about discarding one of those values (blank-identifier).
It's time to get started.
As I said, in this project, I'm going to use a function from the Go Standard Library.
Here it is! Its name is "path". The path package provides utility functions for working with URL path
string (values).
For now, I'm only interested in its "Split" function. It's because I want to separate a URL to
directory and file paths.
Let's take a look at this function in detail now.
Here it is! This function takes "path" as a string.
So it has one input parameter: "path", and it splits the given path somehow, and returns a directory path
and a file name as "string (value)s". So it has two result values: "dir" and "file".
This means that if you call this function it will return two values. Actually, by learning about variables,
you've also learned how to declare input parameters and result values for functions.
Remember that, (when declaring a variable), a type is used (only) once, all variables get the same type.
So this also true here. Both the dir and the file result values are strings here.
It's just like a parallel variable declaration that you've seen previously.
The type of the dir result value is a string and the type of file result value is also a string.
Let's say that I have called this function with this path value. Then this function will return the
directory-path like this, and it will return the filename name like this. As you can see, it splits the given
path and returns it as two string values. The first one is the directory and the second one is the
file.
OK.
Now you know how this function works.
Let's use it in a code example.
In this program, I'm going to split a given URL path to its directory and file name parts.
To do that,
I'm going to use a multiple assignment statement.
First let's declare two new string variables.
"dir" will store the directory path, and "file" will store the filename. Now, let's use these variables in a multiple
assignment like this.
Ok, now, I'm going to call the "path.Split" function with a path value like this.
"css/main.cs". "css" is a directory and "main.css" is the file inside that
directory.
As you know, the Split function returns to string values.
Here, they are being saved into the "dir" and the "file" variables. In this assignment,
the number of variables and the number of values that will be returned from "Split" function are the same.
As you can see, there are two variables on the left-hand side of the assignment statement:
"dir" and "file". And, the path.Split function returns two values.
So the numbers match. OK, let's print these variables
now.
Good! It prints the directory and file names.
By the way, you can also discard some of the values if you don't want to use them, like this.
Let's say that you only want to learn about the filename part of this URL path, but,
you don't care about the directory name. To do that,
you can use a blank identifier in place of the directory variable.
Now, the first returned value from the Split function will be discarded by this blank identifier.
As you can see,
this time, it only prints the filename. It's because there is no longer a directory variable anymore.
The blank identifier has discarded it. Allright.
Now, take a look at the last example.
Actually, you could have used a short declaration instead of an assignment.
So here, I have changed the assignment into a short declaration statement right now.
As you can see, the result is the same, but I think that this code is more concise than the previous version, right?
So you have learned about the path package and its Split function.
You have also learned that when an expression returns multiple values, you can use it either in an assignment
or in a short declaration statement.
That's all for now!
Alright, congrats! You have learned the basics of assignment and that's all for now. See you in the next lecture.