Polymorphism with Golang Interfaces
Polymorphism with Golang Interfaces…
Interfaces in Golang works in a completely different way as compared to Interfaces in other server-side languages. This document will be guiding you with the use cases and scenarios for Golang Interfaces. Before getting in the depth of the Interfaces in Golang, let's start with basic concepts first. Interfaces in Golang provides a list of Function Signatures that are required to be implemented by any “struct” in order to work with specified Interfaces. Let's see what we mean by the above statement below.
If you are new to Golang, probably you need to give the reading to the following article before you start with this one…
Features of Interfaces:
- Interfaces define the functional contract
- Interfaces are Types in Golang
Let's define a simple Interface for an Employee…
In the above code, we are adding 2 functions to the “Employee” interface (“GetDetails” and “GetEmployeeSalary”), any struct/type that needs to work with the “Employee” Interface, should contain these function specified as Interface Contract, in order to use the benefits of Interface.
We will be now defining a new type “Manager” implementing these contract functions so that it can take benefits of “Employee” Interface.
In the code above, we now have a “type” defined which fulfils the contract specified by “Employee” Interface. Now let's look further to see the benefits offered by Interface while working with these newly defined types.
Interfaces Are “Types” in Golang
Interfaces can be used as “types” in Golang, which mean that we can create variables of type “interface” and any struct that fulfils the functional contract of the Interface can be assigned to this “interface” variable.
Once we have any type declared like “Manager” that contains all the functional requirement for the “Employee” Interface contract, we can then create their objects and can assign them to the “interface” variable. Let's see this with an example…
In the above code following things need to be observed:
- We created an Object for struct of type “Manager”
- “Manager” Struct contains all functions required for “Employee” Interface
- Variable of type “Employee” is created
- Manager Object is assigned to the “employeeInterface” variable.
- “employeeInterface” can then be used to invoke functions that belong to Interface of type “Employee”
In the above code, we can see that we created a variable of Interface type and any “Struct” that contains all the functions specified in the Interface contract can be assigned to it. Therefore we were able to assign an Object of type Manager to the Interface of type “Employee”.
Interfaces in Golang are Different…
This feature is quite distinct from the implementation of Interface in any other Server Language. The major takeaways from the above code are:
- The interfaces can be used as types in Golang
- An object having all Interface functions, can be assigned to Interface variable
Let's write the entire scenario in totality...
Polymorphism With Golang Interfaces…
Since we can create a variable of type “interface” and assign “struct” object to it, it provides further added advantage of assigning an object of different types to an interface which fulfils the functional contract and invokes the function from it and hence enabling the feature of Polymorphism.
Let's implement another “struct” type that contains all the functions that are required by Employee Interface. We will be creating a new Type “Lead” that would implement all the functions that are required by the Employee Interface. Since it contains all the functions mentioned in the Interface Contract, we can assign the object to interface variables.
In the above code, we can see that we can assign either object of Lead Type or Manager Type to the interface variable and hence serving as a common interface to invoke the same function belonging to different types and hence ensuring Polymorphism.
Using Interfaces as Function Parameters
The Interface can be added as a parameter to the function. In case it is added as a parameter to the function, the parameter can take any Object as input which satisfies the interface contract and can be then used to invoke functions from it. This is a form of Runtime Polymorphism where the same variable can be used to invoke the function from different Object Types.
Let see this with the help of an example:
Since both the Object Type implements the function belonging to the Interface, they can be passed to any function expecting Interface as Input Parameter.
Using Interfaces as Types in Structs
While defining the new Data type, we can use “Interface” as property type, in case the property type is defined as an interface, any Struct implementing the function contract can be assigned to that property.
Let's see with Simple Examples:
In the above example, we can see that the “Person” struct contains a property “user” of Interface type “Employee”. Let's try assigning different structs “Lead” and “Manager” to the property “user”
The above case uses an Interface as a “struct” property type.
As we saw in the article above, interface behaves is a quite different manner as compared to its working in other languages. I hope you enjoyed the article.
Other Articles from the same Author: