Check out a free preview of the full Go for JavaScript Developers course:
The "Interfaces" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna defines what an interface is, namely a list of methods that describes the behavior of types that implement it.

Get Unlimited Access Now

Transcript from the "Interfaces" Lesson

>> So now let's talk about interfaces. And interfaces are a complex aspect of code but they're super powerful, especially on larger code bases and kind of as you're trying to wrangle code and make sure everything makes sense and is readable. And so if structs define a set of attributes on a type, like a user has a first name and a last name, it is of type user.

[00:00:17] Interfaces are gonna describe a set of behaviors that also define a type. So if you're a user and you can log in or change your first name, stuff like that, any type that can accomplish any of those behaviors is also of a type of that interface. And we'll talk a lot about what that means in the next couple slides.

[00:00:37] Okay, there's a lot going on on the slide, but bear with me, we'll walk through it. So on the top left corner, I've defined two different types. There's a mom struct and a dad struct. And each of these has a set of attributes first name, last name, an array of kids.

[00:00:50] And then in the mom struct we have a book club friends and the dads directory of girl friends. I don't mean to categorize in any negative way, but we'll go with it. So let's say that both of these types have a couple of things that they do. So you'll notice that on the right hand side, we have a couple of functions.

[00:01:07] If you look in the middle, methods that are called on both of these structs. They both can call frequently. And it takes an array of kids and both of them can check if the oven is off. So these are all methods that these types can do. So at the top, we have a type Worrier, which instead of a struct, it's an interface.

[00:01:27] And an interface is a list of methods that are going to describe behavior of particular types. So if a struct can CallFrequently and check if the oven is off, it is also a type Worrier. So on the bottom of the file here, you'll see I've instantiated two different variables.

[00:01:45] Susan is of type mom and Bob is of type dad. And so because Susan is of type mom and a mom struct can call frequently and check if the oven is off, Susan is automatically also of type Worrier, which is an interface. We'll take a second to let that slide step.

[00:02:05] There's a lot going on. And then the quote at the bottom, I found while just digging through the documentation, but it seems to kinda encapsulate the concept of an interface quite well. If it walks like a duck, talks like a duck, swims like a duck, it's probably a duck.

[00:02:22] So if a struct has all of these behaviors, it is also of type interface of all of those behaviors. Cool. So let's kind of focus back on our existing types that live in our code base so far. So we have two types. We've got the user and the group, both of which can describe something.

[00:02:43] You can call describe on both of these types. And since interfaces describe the kind of behavior our types can execute, we can create a type interface called describer, which takes the method describe. And so any type that calls describe is also going to be of type describer interface.

[00:03:03] And so the beauty of this is that instead of having to explicitly list something, something, something. We can create a describe humans function that takes any type that is also of the describer interface. So now we can be a lot more generic and reduce a lot of duplication in code by saying that, hey if you are a type that is also a type describer interface.

[00:03:27] I know you have a describe method therefore I can call this method on you without having to ask like are you user. Are you group, are you some other struct that we haven't created yet that we'll use down the road. This function knows anything that gets passed in will be of type describe or interface that must have the describe method on it.