Welcome back.
In his lecture, I'm going to talk about how to create and initialize areas at the same time.
So let's get started.
First, let's create the same rate that we have created in the last lecture, this court creates an
array variable with four empty string values like this one.
Now let's initialize all the elements of disarray.
Let's assign Kavkaz Revanche to the first element.
Stay golden to the second element.
Everything shipped to the third element.
And lastly, let's assign Cafcass second edition to the last element.
All right, here's the question.
Isn't it cumbersome to initialize the elements of an array like this one?
By one, I mean, first you need to create the array variable and then you need to initialize its elements
one by one using the assignment statements.
Fortunately, there is a better way.
Let's take a look at it.
This is an area litoral.
It creates a new era with the given elements at the same time, then I assigned the new era to the book's
variable here.
It's that simple.
By the way, I don't need to use this normal declaration syntax, I can also use the short declaration.
Let me show you.
This is equal to the previous syntax, however, here, go figure out the book's variables type automatically.
So the book's variables type becomes a string theory with four elements.
So always use this syntax whenever you know about the initial elements.
OK, let's take a look at this area.
Literal syntax in detail, actually, a very literal one of the composite literals.
So what's a composite literal?
In summary, a composite literal creates a composite value like an array and a composite value is a
container or other values.
So here this composite array literally creates a new era value along with its elements.
OK, so first you need to type the composite literals type here.
Go creates new array with four swink elements.
Then I'm initializing the elements of the new area in an element list.
This highlighted part is the element list of the new array.
It starts and ends with curly braces.
However, please do not confuse this with a block.
It's not a block and it doesn't create a new skop.
OK, it just initializes the arrays elements and also notice that we need to separate the elements using
commas.
So each element in this list needs to end with a comma.
You also need to type the last comma is in here because here I have type the elements in multiple lines.
By the way, if I had typed the last element just before the ending culbreath, the last comma wouldn't
be necessary.
For example, here I don't need to type.
The last comma is because here the culebras is next to the last element.
Right?
So you don't have to type the last comma here.
And yet you can also type the composite literals on a single line like this, by the way, here, the
length of this area is four, right?
However, here I'm initializing only the first two elements of Desiré.
Right.
So where are the rest of the elements?
Here's a question for you.
So what would happen when I run this called?
What the areas elements will look like.
OK, let's find out.
So these are literal initializes the first two elements, first dango, initializes the rest of the
elements to their zero values, since this is a citrine, Kyra, so they become Amita's rings.
This happens because the land is for remember, areas are fixed, composite values, unlike some other
languages, go always initializes the elements to their zero values.
This prevents a lot of potential bugs along the road.
And by the way, there is also another useful syntax called the ellipsis operator.
This is what it looks like, three consecutive.
That's that's it.
With this syntax, the number of elements determines the land of the array here.
There are two elements.
So the area is like automatically becomes two as well.
And in the end, this array gets created.
As you can see, there are no zero elements because with this syntax, the array becomes fully initialized.
All right, that's all for now.
Thank you for watching so far.
In the next picture, we're going to refactor the hipster's bookstore to Literals.
See you there by.