So it's time to turn our attention to a concept that might be entirely new to some of you and some of
you may have seen a similar concept in other languages.
And that concept is interfaces and interfaces can be challenging to people the very first time that
they encounter them, but they're really not that hard.
And I think the easiest way to demonstrate what an interface is is to actually build one and implement
that implemented a couple of times.
In really simplest terms, an interface is nothing more than a kind of contract.
An interface says, OK, any type that uses this interface has to follow these rules.
So let's let's let's just actually build one.
That's the simplest approach.
So I'm going to start by defining an interface and I define an interface in much the same way I do,
in much the same way I do.
One of the custom data types that we looked at a couple of years ago, and it's you start with the word
type, keyword type, and this is going to be a type of some name.
And I'm going to call my interface animal.
And it is of type, not struct was as we saw when we were creating the user type.
It's a type interface.
And then I have curly brackets and inside I have this signature for that interface.
And all the signature is, is it lists the functions that an interface, an interface of type animal
must have.
So anything that is of type animal must have, it must implement some of some functions.
So I want to say one function that every animal type must have is says, what does the animal say?
And it's a function that takes no arguments and it returns a string.
OK, and I'm going to give it a second required function.
If you create a type animal, it must implement the says function and it must implement the number of
legs function, which takes no arguments and returns in it there.
I have just defined an interface.
I haven't done anything with it, but I've defined it.
All I'm saying is if you create a type that needs to satisfy the animal interface, it must have these
two functions.
So let's create a variable.
I'll create another type that I'll call this one dog and it's going to be a struct, just the same way
as we do the user type.
And it only has one required field or one field in there and it will be a name of type string.
So I'm going to store the name of the dog in that string, OK, whatever it may be.
And let's put it in here as well.
Breed string.
So the dog type has a name and a breed and then I'm going to create another one type gorilla, which
I better spell.
Right.
And it too is a struct.
And similarly to the dog, it has a name as a field and then it also has color.
I'm not sure what color gorillas gorillas are, but I'll pick one in a little while and it's going to
be it's going to be a string.
And it also has I need something else.
Number of teeth.
And that's neat.
OK, so I've now defined one interface type and two struct types and now I have my main function.
So what I'm going to do in here to start with is create a dog and that dog will be of type dog and I'll
fill out it's necessary fields.
So my dog has a name name and the name will be Samson and the breed will be German Shepherd.
Shepherd.
OK, so I now created this dog variable.
So this dog variable is of type dog, but it does not yet satisfy the interface of type animal because
I've not created these two functions.
So let's do that right now.
Underneath here I will create a function and I will call it give it a receiver type and the receiver
type can be whatever I want.
I'll call it D for dog, but it has to have the name says and it has to take no arguments and it has
to return a string.
And all I'm going to do is return Wolf.
Then I'll create another function to satisfy the second requirement for the animals type it again will
be of the receiver type dog which I've defined up here, and it has to have a name and the name is Number
of Legs and it takes no arguments and it returns in it.
So I'll return for.
All right.
So now I've created this dog variable.
I've implemented the two things necessary to make it off type animal, but I still haven't used the
animal type.
So let's come down here and create another function func and I'll call this one Pritt info.
OK, so print info and it takes an argument, a of type animal.
And it doesn't return anything, it just prints information logged print line, this animal
says, and then I'll call the method that's built into that type animal, a dart says, and has a number
of legs, hips, legs.
All right.
So I've done this.
Let's just pass this dog, which is of type dog, not type animal.
Let's pass it to this function print info, which requires a type animal.
So I can just say print info dog and look, no errors, no errors, even though this function is expecting
it to get a type of animal and I'm passing it a type of dog that works and it works because I may dog
my variable dog, satisfy the requirements for the interface by having both of the required methods
which are specified up here.
Now let's try this.
Let's create a new variable called Gorilla, and it's a type gorilla and I'll fill out its fields.
So Gorilla has a name name King Kong, and it has a color which I'll call black, which I'm not going
to use, but I'm putting it in there just to show that it's a different kind of type and number of teeth.
I have no idea how many they have.
They're going to go with 32 because that's a good even number and you usually have an even number of
teeth.
So I've created this gorilla type.
It's defined up here.
And if I try to pass that, so if I try to pass it, you print info, print info and I pass it gorilla,
it's getting an error and the error is cannot use gorilla type gorilla as type animal type does not
implement animal as some methods are missing and then it links a list to fix that.
I can come down here, copy both of these that are of type dog paste them and change them to type gorilla
and type gorilla and gorillas have two legs and I have no idea what the gorilla says.
So but now that I've implemented the necessary methods for the type animal on my variable gorilla or
my type gorilla, now I can pass that.
And if I actually run this.
So let's go over to my terminal window.
And run it.
Go run, Mingo.
And it should work, this animal says Wolf and has four legs.
This animal says grunts and has two legs.
And I say, I have an extra parentheses in my function probably.
And there it is right there.
But that's all there is to it.
Now, the great thing about this is that I can now create more generic functions that accept a type,
an interface type, rather than a specific type like dog or gorilla.
I can just create one that accepts animal and that's all there is to interfaces.
The key thing to remember is that once you define an interface, anything else can implement that interface
just by implementing the required methods.
That's all you have to do.
There's nothing else.
There's no implements.
Keyword there's no explicitly declaring that it implements a particular interface.
You just implement the required signature and you are good to go.
And this will be incredibly useful to us as time goes on.