Welcome, as you know, string values are only or in other words, immutable values in this lecture,
you're going to learn why they are like so.
All right, let's get started.
Let's take a look at this string literal, you learned that a string value is a slice.
Remember, a slice is a simple data structure that points to a bank.
Incorrect.
So if a street value is also a slice, then it should also point to a back injury, right?
Yes, that's right.
Let's take a look at behind the curtain.
A street value is a tiny data structure called the string Heather.
Here the street value is hello.
It has five bytes, so its length is five.
Whenever you call the land function on a string value, it returns the length of the string just by
looking at this field inside the string hattar so it doesn't calculate the length of a string each time
you want to learn its length.
The third field is the capacity field.
Remember the capacity controls how much a slice can grow, but a string value cannot grow.
It is read only.
So does it really need to have a capacity?
Of course not.
Let's with.
This is what a strong leader looks like, it describes where a street value starts and ends in computer
memory.
It works just like a slice header.
So far, so good.
But I didn't answer the first question.
Why is a street value read only?
Let's say I have to string values and they are equal.
They are pointers point to the same elements in the same back in Gary.
They are Langguth, are also the same as you can see, Gore shares the same back, Gary among them.
It does so because strings are used a lot.
So by doing this, Gore needs to allocate a fewer number of backing.
It aims to keep the memory usage low.
It's all about the efficiency.
Let me show you one more example, let's say I sliced this string value lachsa so its pointer becomes
sixty just three bytes away from the start of the Helo's string.
Instead of creating a new back injury, the new string value uses the same back in.
Correct.
So you can say that God creates a lot of back injuries inside the memory as your program runs, then
it tries to share them among the string headers.
That is why the strings are read only if they were not read, only changing.
Only one of them would break your program.
And it would lead to very hard to fix problems because strings share the same backing arrays.
Let me show you one more example.
Let's say I need to convert the strength to a bad slice here, the conversion creates a new bite slice.
The slice needs a back injury.
However, unlike a drink, a slice is not read-only, so it cannot use the same backing of the string
value.
Because of that, go allocates a new back injury for the slice and it copies the first back, carries
data to the new back injury.
As you know, whenever something is copied or allocated, it will consume computer resources such as
memory, CPU and so on.
So it should be your goal to reduce unnecessary allocations and copies.
Anyway, after creating a new back injury and copying the previous arrest data into it, Gore sets the
slicers pointer to the newly created back injury and it determines that the length of the slice is five
bytes because there are five bytes in the string.
By the way, when you convert the strength to a bite slice, its capacity can be anything.
I mean, its capacity doesn't have to be equal to the length of the converted shrink.
For example, here the capacity becomes 10.
This number may change depending on the go runtime that you are using.
OK, now you are learnt how the strings work behind the scenes.
Now I'm going to show you some concrete examples about the inner workings of strings in the coding,
Ed..
All right, let's get started.
Here I created some court previously, this is what a sprinkler looks like in the real go code, as
I showed you, it has a pointer to the back injury and it determines where a string value starts and
it has a LANGFIELD which determines where the string ends.
I also brought a helper function that helps me to show you how strings work behind the scenes.
It finds the member location of the string value.
Then it converts the value to a string header.
You don't need to understand how this function works right now.
You just need to know how to use it.
And it goes like this.
You pass a string value to it and it shows you what it's doing.
Heather looks like.
Anyway, let me first show you what an empty string value looks like behind the scenes.
I'm going to declare an empty string variable like this, then I'm going to print the fields of its
string header.
OK, let's take a look.
As you can see, this is the street value.
It's an empty string, right?
And this is its pointer, it points to a new value, it doesn't point to anything.
So this means that there isn't a back injury for empty strings and its length is, of course, zero.
OK, let me comment on this one.
Now I'm going to declare another sink value with this, how low value?
Let me show you what it looks like.
This time, it points to a memory address like this, so it has a back injury with five items.
Now I'm going to show you go share the same back injury among the shrinks.
I'm going to pass the same street value.
Hello to the dump function.
Let's take a look.
As you can see, they're seeing headers look exactly the same, right?
However, if I create another string like this one, it's string Heather will be different.
Let's take a look.
As you can see, even adding one more character to a string value changes its backing right here.
God creates a new Badgingarra for this helo's string so it doesn't share the same backing error with
the previous string values here.
Now, I'm going to slice this Helo's drink variable like so let's take a look.
As you can see it, Prince, the first letter, however, look at its pointer.
It uses the same back injury with the previous yellow string value.
So this is a proof that you can slice a string value very efficiently.
When you slice it, there won't be a new allocation.
Let me show you the same thing in a loop so that you can see how efficient slicing a string is.
OK, let's take a look.
As you can see there, Badgingarra is still the same, so slicing a shrink doesn't create a new back
injury.
It's very efficient and cheap.
As the last examples now, I'm going to show you the effect of converting a string to a bite slice and
a bite slice was was like so.
All right, let's try it.
As you can see, all the pointers are looking at different back injuries, so this means that the convergence
allocate new back injuries, compare it to the previous back injury of the hell the variable, it's
completely different.
This means that unless necessary, do not convert strengths or do it only once.
All right, this is how street values work and go behind the scenes by using this knowledge, you can
create more efficient programs.
OK, thank you for watching so far.
Seeing the next lecture by.