Welcome. In this part of the section, I'm going to show you the basics of strings.
In the first step, I'm going to talk about the raw string literals. In the second step, you'll learn how to
combine strings using the concatenation operator. In the third step, you'll learn about how to get the length
of a string.
And the last step, I'm going to show you an example program using strings.
By the way, in this part of the section, you'll get basic understanding of strings.
I'm going to return back to it after the slices section for a more comprehensive explanation.
So are you ready?
Let's get started.
OK.
Let's talk about raw string literals first.
You already saw the string literal here before. It just encloses the characters within the double quotes.
And here's an example of a raw string litera. As you can see here,
this time, you need to wrap the characters between back-quotes instead of double-quotes. A string literal cannot
contain multiple lines of text.
However, unlike a string literal, a raw string literal can contain multiple lines of text.
By the way, the first one, a string literal value is being interpreted by Go.
For example, if there is a new line escape sequence inside a string literal, Go will convert it to a
newline character.
However, a raw string literal is an unprocessed string data.
So Go doesn't interpret anything in it.
It is as it is. For example, if there is a new line escape sequence in it, Go won't see it as an escape
sequence.
Don't worry though, you're going to see how it works in a minute.
The type of a string literal and a raw string literal is the same.
They both are strings but they only typed differently.
As I said, a string literal is typed between double-quotes.
However, a raw string literal is typed between back-quotes.
OK now let me show you a few examples in the coding editor.
Now, I'm going to declare a new string variable like this.
Let's assign it a string literal value like this.
Now I'm going to assign it a raw string literal value like this one. As you can see here, I've wrapped it inside
back-quotes instead of double-quotes.
When I do this,
Go thinks that this is a raw string literal. And, I can assign the raw string literal to a string variable.
Because a raw string literal's default type is string.
Exactly the same as the string literal above.
Now let's print this variable.
Let me show you this in the console.
All right looks ok.
I told you that a raw string literal can contain multiple lines of text right.
Let me show you how useful that can be. First, I'm going to type a small part of HTML code.
By the way, web pages are coded with the markup language called HTML.
I use \n\t here. It's because I want to print this "body tag" in a new line and I use
\t just for the indentation. Then I'm using this \" escape sequence here to print a double-quote.
If I used just double-quotes here instead,
Go would warn me.
Let me show you.
As you can see, Go couldn't recognize this as a string literal.
Let me put it back.
All right.
Now I'm going to print it.
It's nice and pretty, right?
However, this string literal is hard to read and maintain.
It's better to use a raw string literal instead.
So that I don't need to type escape sequences.
Let me show you how to do that.
Now I'm going to use a raw string literal. To do that,
first, I need to type it inside the back-quotes like this.
Then I'm going to type the actual HTML code inside.
Here, I am typing it as an HTML code in a nice and pretty formatted way, right?
I don't need to worry about escape sequences. As you can see,
this string value here contains multiple lines.
In the console,
it looks like this.
Nice and pretty again. And, it's also pretty here, in the code.
Now let's compare the above string literal with this raw string literal here.
I didn't need to use any escape sequences. It's because Go doesn't process what's inside a raw
string literal.
Go takes it as it is. As always,
there are exceptions.
Let's take a look at this example.
Here's a file path.
This is a Windows file path.
In this path, backslashes separate the directories and files.
But, in Go, backslash is an escape character. So that's why I need to use this slash slash escape sequences
here.
Let's take a look at
how can I create the same string value. But this time I'm going to use a raw string literal instead.
Here, I don't need to escape the backslashes.
This is because Go doesn't process what's inside of a raw string literal.
So I can use the backslashes without escaping them. I think that these raw string literals look better than a string literal.
Congrats!
That's all for now. See you in the next lecture.