Interfaces

GetGoing: Introduction to Golang Data types and control structure
6 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hi everyone in this video we're going to be talking about interfaces. So interfaces are named collections of method signatures. So here I am in my main three Dotto and I'm just going to dive into an example and show you how interfaces work. So what I'm going to do is I'm gonna define an interface I'm going to see type God in faith. And what you can do when inside and interfaces, you can define a certain set of functions, which you want other classes to follow, or other structures to follow. So you can say that okay, a car should drive.

Right, and then a car should stop. Yeah, because a garbage doesn't stop is very dangerous. And then I have two types. I have a type Lambo struct, which, in which I can say okay, so Lambo mod, which is a string date and then I want to have a Chevy Because why not and then I'm going to see a should be more than a string so now in my phone's me I just want to call these functions right. So what I can do is I can define these dry methods and shortstop methods on top of these other drugs and then they will start following the interface so I can simply say func straw Lambo Lambo, and then I can say dry. I can see it from incline.

Lambo on the moon and then I can do the same thing for a Chevy. I can say Chevy Right. I can see Chevy on the move. I can also say which model my Chevy is on the move I can say see doored Chevy mortar. And here I'm going to say l dot lambda model. So, now let's get to the fun and inside my funk meet and what I'm going to declare two new dice one is so Lambo in the demo, I'm gonna say, okay, the Lambo model is let's say, Florida.

And I'm gonna fish Chevy Chevy model is C 369. I'm not sure if this exists. So then I'm just gonna do l drive. I'm gonna do c dot right So let's see what happens. So I'm going to do a golden mean three dot undefined FMT. So, obviously, because we didn't import different import format package, it's gonna work.

There you go, Lambeau in the garden. So one obvious benefit that you might see from this example was that we can define a set of instructions, a set of structure, so that both of these models are kind of consistent with the whole structure. So if you're talking about a car, a car should drive a car should stop, or a car should do other things. If we're, if you want to build a game, you might have a character who walks he might have a character who talks. So walking and talking might be defined in an interface. And then there, they can be very different types of characters.

They can be NPCs playable characters, but all of those characters can follow those certain set of things. faces. So if you look at this dashboard right here, you know, draw.io board right? You can easily see that okay, I had a car interface, which both the Chevy and the Lambo follow. So this enforces a kind of consistency and it makes your code very readable and very programmable because you know that okay? If you're talking about Chevy, if you're, if you're talking about any car, they will follow this interface.

Then, if they're following this interface, you automatically automatically know that they have the drive and the stock method. And then one thing you can do is you can enforce it to use the interface. So you can have something like funk new model, which returns a Lambo model, which returns a god interface and then you can say return Lambo with any odd So you can do this also. So here we're gonna add a string. And then so you might notice that the Lambo loses the drive method, but it doesn't invoke the stop method, it doesn't define this method. So if you if you try to run it again, it's gonna say that okay, interface I failed, because it's not it does not have the stock method.

So, this is a kind of a very neat way to make sure that all of your class all of your structures, they use the interface or I can I can simply make a new function I can say, start Lambo and then I can say stop, like, so installed I can save time stopping. Good. So, while So, you kind of saw the power of interfaces over here, so If you if you are returning a new model right installed, instead of returning the type of the model just return the interface. So what this means is that okay, I have this model, but this model needs to satisfy this interface else don't compile my code. So you looked at the Lambo struct. So it It should follow the car interface.

If it doesn't follow the car interface, then it's going to throw an arrow. So the car interface is driving stop. So this is a very neat way to kind of make sure that all of your functions, they follow a set of instructions that you want them to follow a set of methods in which you want them to invoke. So I'll see you in the next video with some more examples on interfaces.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.