So we've already learned something about strokes, but there's another thing you need to know about
them, and that is that strokes can actually have functions associated with them.
So right now, I have an empty go project with one file, Mango, and as you can see, it just has one
empty main function.
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
struct of type struct and we'll give its definition and it's going to have one field in it and the field
is going to be called first name and it's a type string.
So I've now created that type.
So now I can create a variable of that type and we'll create one or we'll call the ver my father.
It's a type my struct.
So I've created an empty variable and now I can assign something to first name just by going.
My very first name equals John and that's easy enough.
And as you know, I can also create a second variable so I could create a via my var too.
But I can do it also using shorthand.
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
fill it.
First name will do it all in one one step, create the variable and assign values to its members in
one step.
And this is a very common shorthand.
So I could say first name and that's going to be equal to marriage.
So I've now created my Vata and I've created my VAR haven't done anything with my Vata, so there's
an error.
So let's just print some information out.
Log dot print line.
My var is set to and we'll just print my very first name and then I can duplicate that line and say
my Vertu is set to my var to first name.
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
it to do.
It'll print out two lines of text with the first name, first name from the my version and the first
name from my var too.
So go run may not go away.
First my version is set to John, my vata is set to marry but I can assign a function to this struct
and I can do that really easily.
So let's first of all create a new function.
Function defines a function.
But rather than just giving the function name, which could be something like the print first name and
return a string, we'll just do it this way first.
Well, I have no values to return, so I'd actually have to hard code a string here.
But if I put right after the funky keyword in parentheses some variable name and then a pointer to the
struct in question, my struct, now I have access to this variable.
And what this does, this is called a receiver and it ties this function to the type of my struct because
I'm using my struct a pointer to my struct here points to this, this, this type and that means I have
access to information from my struct so I can say return my first name.
And now this function or this this type, my struct actually has a function associated with it.
So when I assign the values here, I did it manually and that's great.
And I printed it this way.
But I can also do this.
Watch this.
I can say my var dot print, first name and my var to.
Print first name, so I'm calling a function that's part of this struct, and the way that the function
is tied to that struct is by using this receiver.
So if I clear the screen in my terminal and run this, I get exactly the same results.
But rather than accessing the member directly, I'm calling a function.
Now, the good thing about this, of course, is that this function can be far more complex than merely
returning the value of a given member.
You can perform some business logic in here and have that business logic actually become part of this
struct.
And that is exceptionally useful and we'll be using that regularly throughout the remainder of this
course.