WEBVTT 00:01.690 --> 00:07.760 In this video we will learn what the struct and method receivers are how to define and call them. 00:07.930 --> 00:10.450 Consider struct as complex type. 00:10.450 --> 00:17.570 They are a composition of many other types on the previous example we saw a map of string the strings 00:17.840 --> 00:20.660 representing a user with four elements. 00:20.660 --> 00:27.560 Name row email and age while name roll and email where strings. 00:27.590 --> 00:31.580 Age isn't really represented properly as a string. 00:31.580 --> 00:33.320 Age is probably more a number. 00:33.980 --> 00:41.270 So map is good for some cases but maybe it's not always flexible enough to model some of their use because 00:41.270 --> 00:42.110 we might have. 00:42.710 --> 00:48.070 So let's model the user using struct to define this track. 00:48.080 --> 00:55.160 The first thing you have to do is to define a new type using the type keyword followed by the name of 00:55.160 --> 00:55.950 the type. 00:56.000 --> 00:58.910 In this case let's use user. 00:59.330 --> 01:03.040 And finally we're saying that the new type it's a struct. 01:03.160 --> 01:08.000 So we type a struct open a closed square brackets. 01:08.000 --> 01:11.260 So now we have a user type which is a struct. 01:11.280 --> 01:15.390 Now let's define some fields in it. 01:15.650 --> 01:22.410 Let's say a name which is a type of string role an email. 01:23.210 --> 01:29.100 So all this three fields are string for simplicity. 01:29.300 --> 01:31.100 As all of them are the same type. 01:31.100 --> 01:40.650 I can define them online the same as we did with function arguments and now age it's an INT. 01:41.040 --> 01:48.620 So now I have my user model or struct with four fields three of those are strings. 01:48.670 --> 01:50.260 And the last it's an INT. 01:50.260 --> 01:58.570 So how should I initialized user by just calling user in open and close square brackets. 01:58.570 --> 02:04.450 Now if I want to populate some of this value I just use a name which is the name of the field that I 02:04.450 --> 02:05.720 want to use. 02:06.160 --> 02:10.910 Let's say Gustavo which is the same value from the previous example. 02:11.010 --> 02:22.010 Row developer an age which in this case is a number so we have the same fields and an object with that 02:22.010 --> 02:23.020 on the previous example. 02:23.030 --> 02:29.270 But this time they are properly types name and role are strings and age. 02:29.270 --> 02:29.990 It's an INT. 02:30.140 --> 02:33.970 Now I'm not specifying email because I just don't want to. 02:34.080 --> 02:38.420 Let's bring this to get a better picture of what's happening 02:41.330 --> 02:42.320 so yeah. 02:42.340 --> 02:47.590 Gustavo developer 30 it doesn't specify the fields here but I know them. 02:48.590 --> 02:58.930 Now I can also access independent field by using the dog notation so I'm gonna use user dot name to 02:59.080 --> 02:59.510 the name 03:03.100 --> 03:08.740 or role to bring the role right now. 03:08.740 --> 03:12.610 Note that I'm not using the square bracket notation here. 03:12.670 --> 03:13.590 This will fail. 03:15.730 --> 03:24.610 As well as this basically because the user type does not support indexing asks slices and maps. 03:24.690 --> 03:33.020 So OK this is how I define a structure and this is how I initialize a structure now. 03:33.040 --> 03:39.910 The nice thing about structure is not only that you can pack different values but that I can do operations 03:39.910 --> 03:40.620 over those. 03:40.660 --> 03:45.600 So I do that by defining methods. 03:45.610 --> 03:54.920 So let's say I want to define a method over the user structure images remove this error. 03:55.120 --> 03:59.110 How do I do that is that I use the fun keyword. 03:59.530 --> 04:06.770 But in this case I will say that the function that I'm gonna define is going to be handle by the user 04:06.770 --> 04:07.880 structure. 04:07.930 --> 04:09.850 So I only do it like this. 04:09.850 --> 04:16.940 I'm saying this is operating over the user and I'm gonna reference the user using you. 04:16.990 --> 04:20.620 This is not my made clear now but you will pick it up. 04:21.520 --> 04:27.730 Now let's say I want to define a method receiver call salary which is not gonna return any argument 04:27.860 --> 04:29.530 that is going to return a number. 04:29.710 --> 04:30.090 Right. 04:30.370 --> 04:33.620 So again I have my user reference. 04:33.630 --> 04:37.080 Are you gonna have a salary method. 04:38.060 --> 04:40.520 Let's do some behavior here. 04:40.520 --> 04:46.030 So let's say he is sweet you roll. 04:46.130 --> 04:48.290 Now you see what you does right. 04:48.340 --> 04:57.120 Reference the user in case it's developer let's return a hundred and if it's an architect. 04:59.580 --> 05:02.360 Let's do it to 200 05:06.490 --> 05:11.590 otherwise let's just return zero because we don't know how to handle that role. 05:12.230 --> 05:12.990 Okay. 05:15.070 --> 05:17.140 So how should I call this method. 05:18.640 --> 05:30.600 I already have a user which is a developer so let's print it salary let's say user dot salary. 05:30.680 --> 05:38.800 Remember two access fields we use the dot notation and to use methods we also use this use the dot notation. 05:38.800 --> 05:43.330 The difference is that at a salary is a function. 05:43.330 --> 05:45.490 I have to open and close parenthesis. 05:47.030 --> 05:51.930 Okay so I have defined our user structure with this field. 05:51.950 --> 05:58.270 Now I'm calling the salary man not over the user with capital. 05:58.850 --> 06:08.390 I'm calling it over the variable which is user or let's say thus OK now let's execute this and see what 06:08.420 --> 06:09.890 happens OK. 06:10.100 --> 06:22.470 As Gustave is a developer he salaries hundred now let's say John it's also a user he's an architect. 06:22.530 --> 06:26.270 I really don't care about your age for this example. 06:26.340 --> 06:33.900 So John salary to better understand this. 06:33.920 --> 06:43.460 Let's type the name and John name right. 06:43.490 --> 06:45.320 So we have two different users. 06:45.320 --> 06:49.730 One is assigned to the ghost variable and the other one is assigned to the john variable. 06:49.730 --> 06:57.200 And then we're painting the name which is an attribute or a field and salary which is a function or 06:57.200 --> 07:01.490 a method receiver you would have to have a hundred. 07:01.490 --> 07:03.700 JOHN Two hundred. 07:03.690 --> 07:11.920 Now the last thing that I want to talk about is how to modify internal data structure with methods. 07:11.920 --> 07:18.960 This might be a little complicated because it involves pointer so let's say once we find a method the 07:18.960 --> 07:29.070 receiver will update the e-mail of a useful let's these and let's call update email he's going to receive 07:29.070 --> 07:33.260 an email stream and it's not going to return anything. 07:34.800 --> 07:38.930 Let me just remove this and go back to user right. 07:39.030 --> 07:48.310 So what I should do is you've probably already guessed you that e-mail is gonna be equal to e-mail. 07:48.360 --> 07:49.260 Right. 07:50.190 --> 07:55.440 So now let's call that you talk about updating e-mail. 07:56.070 --> 08:01.520 Is going to be a travel company dot com. 08:01.850 --> 08:03.910 And now let's print. 08:05.250 --> 08:14.410 What should happen and it should happen because not gonna happen is that now the user structure half 08:15.780 --> 08:18.690 an email is specified but it hasn't. 08:18.750 --> 08:24.450 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 08:25.500 --> 08:32.970 and this is because every time I define a function or a method receiver when I'm passing is a copy of 08:32.970 --> 08:34.350 the user structure. 08:34.410 --> 08:35.250 Right. 08:35.250 --> 08:43.320 So if I want to reference the exact user structure I need to say that this is a pointer and I do that 08:43.320 --> 08:44.180 by using this. 08:45.390 --> 08:53.040 So now I'm saying that instead of passing a copy of the user to the update email and passing a pointer 08:53.520 --> 08:59.630 to the original user value variable sorry. 08:59.640 --> 09:03.860 So now if I run this I can see that the email. 09:04.230 --> 09:11.770 So this might be a little bit complicated but in some languages like C you have pointers and values. 09:11.850 --> 09:13.290 The important thing is. 09:13.410 --> 09:20.850 Or use it as a rule when you're just reading values like salary you're just reading you that roll. 09:20.850 --> 09:23.340 You're not modifying. 09:23.340 --> 09:33.520 You can use values not pointers but when you're updating your structure you have to use pointer to access 09:34.000 --> 09:34.890 the same value. 09:35.410 --> 09:42.490 And again we're not going to go into very deep details on these if you want more informations always 09:42.490 --> 09:46.860 go to the official goal and documentation. 09:46.900 --> 09:52.160 It's pretty pretty well written and you will have lot of information there. 09:52.270 --> 09:56.410 So that's it for four structures. 09:56.680 --> 10:03.230 Try to do some exercises around it because we're going to be using it heavily on next episodes. 10:03.250 --> 10:03.640 Thanks.