What is an interface?
In Go, an interface is a set of method signatures. When a type provides the definition for all the methods in the interface, it is said to implement the interface. It is much similar to the OOP world. An interface specifies what methods a type should have and the type decides how to implement these methods.
Now let’s look at practical use of interface.
We will write a simple program that calculates the total expense for a company based on the individual salaries of the employees. For brevity, we have assumed that all expenses are in USD.
package main
import (
"fmt"
)
type SalaryCalculator interface {
CalculateSalary() int
}
type Permanent struct {
empId int
basicpay int
pf int
}
type Contract struct {
empId int
basicpay int
}
//salary of permanent employee is the sum of basic pay and pf
func (p Permanent) CalculateSalary() int {
return p.basicpay + p.pf
}
//salary of contract employee is the basic pay alone
func (c Contract) CalculateSalary() int {
return c.basicpay
}
/*
total expense is calculated by iterating through the SalaryCalculator slice and summing
the salaries of the individual employees
*/
func totalExpense(s []SalaryCalculator) {
expense := 0
for _, v := range s {
expense = expense + v.CalculateSalary()
}
fmt.Printf("Total Expense Per Month $%d", expense)
}
func main() {
pemp1 := Permanent{
empId: 1,
basicpay: 5000,
pf: 20,
}
pemp2 := Permanent{
empId: 2,
basicpay: 6000,
pf: 30,
}
cemp1 := Contract{
empId: 3,
basicpay: 3000,
}
employees := []SalaryCalculator{pemp1, pemp2, cemp1}
totalExpense(employees)
}
Output
Total Expense Per Month $14050
We have two kinds of employees in the company, Permanent
and Contract
defined by structs in lines no. 11 and 17. The salary of permanent employees is the sum of the basicpay
and pf
whereas for contract employees it's just the basic pay basicpay
. This is expressed in the corresponding CalculateSalary
methods in line. no 23 and 28 respectively. By declaring this method, both Permanent
and Contract
structs now implement the SalaryCalculator
interface.
The totalExpense
a function declared inline no.36 expresses the beauty of interfaces. This method takes a slice of the SalaryCalculator interface []SalaryCalculator
as a parameter. In line no. 59 we pass a slice that contains both Permanent
Contract
types to the totalExpense
function. The totalExpense
function calculates the expense by calling the CalculateSalary
method of the corresponding type. This is done in line. no 39.
The biggest advantage of this is that totalExpense
can be extended to any new employee type without any code changes. Let's say the company adds a new type of employee Freelancer
with a different salary structure. This Freelancer
can just be passed in the slice argument totalExpense
without even a single line of code change to the totalExpense
function. This method will do what it's supposed to do as Freelancer
will also implement the SalaryCalculator
interface :).
If you found this helpful, make sure to show your support with a clap, and if you want to help others in their projects, a share would be greatly appreciated! Let me know what you think about this! Happy Learning!