In this video we will learn what the struct and method receivers are how to define and call them.
Consider struct as complex type.
They are a composition of many other types on the previous example we saw a map of string the strings
representing a user with four elements.
Name row email and age while name roll and email where strings.
Age isn't really represented properly as a string.
Age is probably more a number.
So map is good for some cases but maybe it's not always flexible enough to model some of their use because
we might have.
So let's model the user using struct to define this track.
The first thing you have to do is to define a new type using the type keyword followed by the name of
the type.
In this case let's use user.
And finally we're saying that the new type it's a struct.
So we type a struct open a closed square brackets.
So now we have a user type which is a struct.
Now let's define some fields in it.
Let's say a name which is a type of string role an email.
So all this three fields are string for simplicity.
As all of them are the same type.
I can define them online the same as we did with function arguments and now age it's an INT.
So now I have my user model or struct with four fields three of those are strings.
And the last it's an INT.
So how should I initialized user by just calling user in open and close square brackets.
Now if I want to populate some of this value I just use a name which is the name of the field that I
want to use.
Let's say Gustavo which is the same value from the previous example.
Row developer an age which in this case is a number so we have the same fields and an object with that
on the previous example.
But this time they are properly types name and role are strings and age.
It's an INT.
Now I'm not specifying email because I just don't want to.
Let's bring this to get a better picture of what's happening
so yeah.
Gustavo developer 30 it doesn't specify the fields here but I know them.
Now I can also access independent field by using the dog notation so I'm gonna use user dot name to
the name
or role to bring the role right now.
Note that I'm not using the square bracket notation here.
This will fail.
As well as this basically because the user type does not support indexing asks slices and maps.
So OK this is how I define a structure and this is how I initialize a structure now.
The nice thing about structure is not only that you can pack different values but that I can do operations
over those.
So I do that by defining methods.
So let's say I want to define a method over the user structure images remove this error.
How do I do that is that I use the fun keyword.
But in this case I will say that the function that I'm gonna define is going to be handle by the user
structure.
So I only do it like this.
I'm saying this is operating over the user and I'm gonna reference the user using you.
This is not my made clear now but you will pick it up.
Now let's say I want to define a method receiver call salary which is not gonna return any argument
that is going to return a number.
Right.
So again I have my user reference.
Are you gonna have a salary method.
Let's do some behavior here.
So let's say he is sweet you roll.
Now you see what you does right.
Reference the user in case it's developer let's return a hundred and if it's an architect.
Let's do it to 200
otherwise let's just return zero because we don't know how to handle that role.
Okay.
So how should I call this method.
I already have a user which is a developer so let's print it salary let's say user dot salary.
Remember two access fields we use the dot notation and to use methods we also use this use the dot notation.
The difference is that at a salary is a function.
I have to open and close parenthesis.
Okay so I have defined our user structure with this field.
Now I'm calling the salary man not over the user with capital.
I'm calling it over the variable which is user or let's say thus OK now let's execute this and see what
happens OK.
As Gustave is a developer he salaries hundred now let's say John it's also a user he's an architect.
I really don't care about your age for this example.
So John salary to better understand this.
Let's type the name and John name right.
So we have two different users.
One is assigned to the ghost variable and the other one is assigned to the john variable.
And then we're painting the name which is an attribute or a field and salary which is a function or
a method receiver you would have to have a hundred.
JOHN Two hundred.
Now the last thing that I want to talk about is how to modify internal data structure with methods.
This might be a little complicated because it involves pointer so let's say once we find a method the
receiver will update the e-mail of a useful let's these and let's call update email he's going to receive
an email stream and it's not going to return anything.
Let me just remove this and go back to user right.
So what I should do is you've probably already guessed you that e-mail is gonna be equal to e-mail.
Right.
So now let's call that you talk about updating e-mail.
Is going to be a travel company dot com.
And now let's print.
What should happen and it should happen because not gonna happen is that now the user structure half
an email is specified but it hasn't.
If you see I see travel which is the name I see the role and I see the H but I can't see the e-mail
and this is because every time I define a function or a method receiver when I'm passing is a copy of
the user structure.
Right.
So if I want to reference the exact user structure I need to say that this is a pointer and I do that
by using this.
So now I'm saying that instead of passing a copy of the user to the update email and passing a pointer
to the original user value variable sorry.
So now if I run this I can see that the email.
So this might be a little bit complicated but in some languages like C you have pointers and values.
The important thing is.
Or use it as a rule when you're just reading values like salary you're just reading you that roll.
You're not modifying.
You can use values not pointers but when you're updating your structure you have to use pointer to access
the same value.
And again we're not going to go into very deep details on these if you want more informations always
go to the official goal and documentation.
It's pretty pretty well written and you will have lot of information there.
So that's it for four structures.
Try to do some exercises around it because we're going to be using it heavily on next episodes.
Thanks.