1
00:00:00,870 --> 00:00:04,920
So we've already learned something about strokes, but there's another thing you need to know about

2
00:00:04,920 --> 00:00:08,490
them, and that is that strokes can actually have functions associated with them.

3
00:00:09,300 --> 00:00:14,820
So right now, I have an empty go project with one file, Mango, and as you can see, it just has one

4
00:00:14,820 --> 00:00:15,790
empty main function.

5
00:00:16,320 --> 00:00:22,200
So let's first of all, create a struct we'll create a type that is a struct and I'll call it type my

6
00:00:22,200 --> 00:00:29,520
struct of type struct and we'll give its definition and it's going to have one field in it and the field

7
00:00:29,520 --> 00:00:32,690
is going to be called first name and it's a type string.

8
00:00:33,960 --> 00:00:35,430
So I've now created that type.

9
00:00:35,580 --> 00:00:42,300
So now I can create a variable of that type and we'll create one or we'll call the ver my father.

10
00:00:42,870 --> 00:00:45,060
It's a type my struct.

11
00:00:46,080 --> 00:00:50,510
So I've created an empty variable and now I can assign something to first name just by going.

12
00:00:50,520 --> 00:00:57,360
My very first name equals John and that's easy enough.

13
00:00:58,050 --> 00:01:03,180
And as you know, I can also create a second variable so I could create a via my var too.

14
00:01:03,450 --> 00:01:05,220
But I can do it also using shorthand.

15
00:01:05,220 --> 00:01:13,470
I can just say my var too is assigned the value of and it's going to be a type my struct and we'll just

16
00:01:13,470 --> 00:01:13,860
fill it.

17
00:01:13,860 --> 00:01:19,260
First name will do it all in one one step, create the variable and assign values to its members in

18
00:01:19,260 --> 00:01:19,740
one step.

19
00:01:19,740 --> 00:01:21,270
And this is a very common shorthand.

20
00:01:21,810 --> 00:01:24,980
So I could say first name and that's going to be equal to marriage.

21
00:01:27,300 --> 00:01:32,910
So I've now created my Vata and I've created my VAR haven't done anything with my Vata, so there's

22
00:01:32,910 --> 00:01:33,270
an error.

23
00:01:33,270 --> 00:01:34,740
So let's just print some information out.

24
00:01:35,400 --> 00:01:37,500
Log dot print line.

25
00:01:38,970 --> 00:01:48,690
My var is set to and we'll just print my very first name and then I can duplicate that line and say

26
00:01:48,690 --> 00:01:52,250
my Vertu is set to my var to first name.

27
00:01:52,650 --> 00:01:57,870
Now that I've used my var to the error goes away so I can run this and it'll do exactly what we expect

28
00:01:57,870 --> 00:01:58,290
it to do.

29
00:01:58,290 --> 00:02:04,350
It'll print out two lines of text with the first name, first name from the my version and the first

30
00:02:04,350 --> 00:02:05,760
name from my var too.

31
00:02:05,790 --> 00:02:09,600
So go run may not go away.

32
00:02:09,600 --> 00:02:17,190
First my version is set to John, my vata is set to marry but I can assign a function to this struct

33
00:02:17,310 --> 00:02:18,620
and I can do that really easily.

34
00:02:19,110 --> 00:02:20,910
So let's first of all create a new function.

35
00:02:21,780 --> 00:02:23,490
Function defines a function.

36
00:02:23,490 --> 00:02:30,960
But rather than just giving the function name, which could be something like the print first name and

37
00:02:30,960 --> 00:02:33,320
return a string, we'll just do it this way first.

38
00:02:34,140 --> 00:02:37,650
Well, I have no values to return, so I'd actually have to hard code a string here.

39
00:02:38,130 --> 00:02:45,150
But if I put right after the funky keyword in parentheses some variable name and then a pointer to the

40
00:02:45,150 --> 00:02:49,770
struct in question, my struct, now I have access to this variable.

41
00:02:49,770 --> 00:02:57,000
And what this does, this is called a receiver and it ties this function to the type of my struct because

42
00:02:57,000 --> 00:03:03,120
I'm using my struct a pointer to my struct here points to this, this, this type and that means I have

43
00:03:03,120 --> 00:03:09,300
access to information from my struct so I can say return my first name.

44
00:03:10,050 --> 00:03:16,200
And now this function or this this type, my struct actually has a function associated with it.

45
00:03:16,770 --> 00:03:20,460
So when I assign the values here, I did it manually and that's great.

46
00:03:20,610 --> 00:03:21,930
And I printed it this way.

47
00:03:21,930 --> 00:03:23,280
But I can also do this.

48
00:03:23,580 --> 00:03:24,300
Watch this.

49
00:03:24,990 --> 00:03:31,110
I can say my var dot print, first name and my var to.

50
00:03:33,050 --> 00:03:39,470
Print first name, so I'm calling a function that's part of this struct, and the way that the function

51
00:03:39,470 --> 00:03:42,890
is tied to that struct is by using this receiver.

52
00:03:43,160 --> 00:03:49,130
So if I clear the screen in my terminal and run this, I get exactly the same results.

53
00:03:49,280 --> 00:03:53,570
But rather than accessing the member directly, I'm calling a function.

54
00:03:53,600 --> 00:03:57,800
Now, the good thing about this, of course, is that this function can be far more complex than merely

55
00:03:57,800 --> 00:03:59,960
returning the value of a given member.

56
00:04:00,170 --> 00:04:06,470
You can perform some business logic in here and have that business logic actually become part of this

57
00:04:06,470 --> 00:04:07,040
struct.

58
00:04:07,220 --> 00:04:11,300
And that is exceptionally useful and we'll be using that regularly throughout the remainder of this

59
00:04:11,300 --> 00:04:11,720
course.
