From the course: Transition from Java to Go

Unlock the full course today

Join today to access over 24,100 courses taught by industry experts.

Interfaces

Interfaces

- [Instructor] We already know that structs don't support inheritance, so let's now have a look at how we can achieve polymorphism with interfaces. We can think of interfaces as named collections of methods or behaviors. Unlike Java, Go does not have an implements keyword. Structs simply implement interfaces by implementing the methods that they define. The compiler will check and enforce interface types where needed. Being able to define and implement new interfaces without needing to modify existing code gives Go great flexibility and makes interfaces lightweight. Similar to structs, we define interfaces using the type and interface keywords separated by the name of the interface. For example, the syntax type CityTemp interface defines an interface named CityTemp, short for CityTemperature. Structs can implement multiple interfaces. This is one of the reasons why they are very powerful polymorphism mechanisms. A…

Contents