Check out a free preview of the full Basics of Go course

The "Interfaces" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates how to create interfaces in Go, which define a set of methods that can be used as a type. Interfaces in Go have implicit implementation and can emulate polymorphism commonly found in object-oriented programming.

Preview
Close

Transcript from the "Interfaces" Lesson

[00:00:00]
>> But, not everything is good news, because what if we wanna create here an array of courses. So I create courses, and I say it's going to be a variable, and it's an array of, let's say, two. Two what? 2 courses and then we say okay, the first one, let's do that at the end after defining them.

[00:00:30]
I'm sorry Kyle, I will delete you for a while, nothing personal, but we don't need you now. So let's do this, so it's getting better. So zero is going to be the core Go course and then the other one it's going to be the Swift workshop, okay? The problem is that in fact it's suggesting me something, what's the problem?

[00:00:57]
That Swift workshop is not a course. It's not real inheritance. It's a workshop, it's a different type. So, and I want this because instead of printing one by one, maybe what I want is to make a loop here and use a for and we can use this, we can say for each index that cast a course that our range you use for range, we use the keyword range and the collection.

[00:01:34]
So for each course What he wants to do is to print or format print F. Anyway, I don't need to use print f with this I can just use print line. It will take the same string method, print line and print line course. You can see that this for range, receives the index, the numbers of 0, 1, 2, blah, blah, blah, and the element.

[00:02:02]
If you don't need the number, you can use underscore. If you don't need the index. Okay, but we have this problem. It's a big problem. We cannot make an array that includes both worships and courses. Do you all understand the problem? Okay, let's see how to solve the problem.

[00:02:25]
To solve the problem, we go back to the idea of interfaces, that was defined here as the final type that we can create. An interface is a definition of methods, it's a list of methods. And we can use polymorphism, or at least emulate polymorphism with this. Let's see them in action, how it can help here.

[00:02:52]
So for example, we can say that I mean, it's not necessary to add this method for you to get the better idea, I'm going to add a method on the course that, so it's a method, so we have this special thing, that will, for example, sign up. It will you can ask for a sign-up.

[00:03:20]
They will return an eyeball wherever. You're a dental, yeah, you're in. Okay, so sign up to the course. So and of course the workshop can have the same as well. And also it can have its own signup. But if I try to do that, it says, hey, well this is course, so you shouldn't change, it doesn't matter the file where it's in the same package, okay?

[00:03:45]
So I can change the implementation here. So both cores and workshops they have signed up, okay? Make sense? So now I can go to the same package. Typically, we create a new file for this. I'm going to create an interface. First, it's a new file. We start always with package and the name of the package.

[00:04:09]
And then we create a new type that we can call it as Signable or kind of course, kind of sign up, any name will work. You will understand the name, I mean, it's like signable, something that you can sign to, and it's an interface. And what you put here in the interface, at least of methods without any code, just the signature of the method.

[00:04:38]
In our case, it's going to be sign up boot. So I can say I wanted to have a function with a name, signup bowl. Okay, something like this. I still need to work with this, okay? So let me show you the idea here, how it looks like, because you can see that it's complaining because I have one more step that the fun keyword is not necessary here.

[00:05:06]
So an interface is just a list of methods. And then we can use that as a type. You say, okay, if you have ever played with Java, Czar, PHP, Swift, Kotlin, they have interfaces or protocols. It's kind of something similar. I really like the name protocol. It's not the Go name.

[00:05:27]
But I like the name too, and understand what's going on. So we are defining a protocol. And then we can see which structures are following the protocol are implementing the interface. So in this case, I don't need the fun keyword. That was the thing. So it's just the same function declaration without the fun keyword, Just that.

[00:05:48]
So now I can go to my array and say, you know what, it's not going to be a ray of course, because if I say so, it will be only instances of that structure. I'm gonna say, they're gonna be signables. Now it works, I don't have any errors.

[00:06:16]
And I am seeing your faces even I'm imagining the faces over the camera like, but wait a minute. Where did I set that course I worship are implementing that interface. I'm not saying it anywhere. In Go, it's implicit by the name or the signature of your interface. So, if you have any type of with that method, with that signature you're in, you're in the interface automatically.

[00:06:55]
You don't need to say so. You don't need to say implement signable. No, it just works. Any type with a method, sign up, return your bull will be in automatically. That's what is known as, do I say here, implicit implementation. So let's run this because I didn't run this yet, and now it works, both are working properly.

[00:07:30]
Because both are having that method so I can create an array, a collection of them. That's an interface, and that's polymorphism. Looping through a lot of objects without actually knowing the kind of objects, I just need to know that it has the function or the action that I need.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now