Polymorphism with Golang Interfaces

Polymorphism with Golang Interfaces…

Mayank Gupta
Dec 11, 2019 · 4 min read

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:

  1. Interfaces define the functional contract
  2. Interfaces are Types in Golang

Let's define a simple Interface for an Employee…

https://gist.github.com/Mayankgupta688/896534b9d099d643a74a6192ae3b0f3e

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.

https://gist.github.com/Mayankgupta688/9ca99460199cbfc26db082c15e9115c6

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…

https://gist.github.com/Mayankgupta688/f2710096cd47b2b02f740e0ca107c4fa

In the above code following things need to be observed:

  1. We created an Object for struct of type “Manager”
  2. “Manager” Struct contains all functions required for “Employee” Interface
  3. Variable of type “Employee” is created
  4. Manager Object is assigned to the “employeeInterface” variable.
  5. “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:

  1. The interfaces can be used as types in Golang
  2. An object having all Interface functions, can be assigned to Interface variable

Let's write the entire scenario in totality...

https://gist.github.com/Mayankgupta688/f349f70e3bd24cab758941dc61d2bd9e

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.

https://gist.github.com/Mayankgupta688/107578d1f8f6d2f137ecbde85380df15

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:

https://gist.github.com/Mayankgupta688/cdfde1c6666d165c1a7761704646cb5d

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:

https://gist.github.com/Mayankgupta688/67f0a13ec83e13a95397cc6165f8f6e9

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”

https://gist.github.com/Mayankgupta688/f29fc92909582172fa0b0277b672c75e

The above case uses an Interface as a “struct” property type.

Conclusion:

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:

Mayank Gupta

Written by

9 Years of Experience with Front-end Technologies and MEAN Stack. Working on all Major UI Frameworks like React, Angular and Vue.

More From Medium

More from Mayank Gupta

Related reads

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade