Interfaces in GOlang
By: Sajal Dulal
Tags: Interfaces, Development, Beginner, Golang, Tutorial

INTERFACES

Before learning anything about what interfaces are, lets take a look at an example to clearly get the idea regarding why we need interfaces and how it could save us a lot of trouble while coding with GO.

Create a new directory and inside it create a main.go file. Paste the following code inside it.

package main import "fmt" type grade11Marks struct {

math int

physics int

}

type grade12Marks struct {

math int

computer int

} func main() { ram := grade11Marks{50, 80}

shyam := grade12Marks{60, 70} ram.printMarks()

shyam.printMarks()

} func (m grade11Marks) printMarks() {

fmt.Println("math: ", m.math, " physics: ", m.physics)

} func (g grade12Marks) printMarks() {

fmt.Println("math: ", g.math, " physics: ", g.computer)

}

Go ahead and run the code with the command go run main.go.

Basically, we declare two different types of struct grade11Marks and grade12Marks and with one variable for each ram and shyam respectively.

If you do not have knowledge regarding structs in GO please refer to the previous article that cover it.

We create a receiver function named printMarks() for each struct separately which are used to print the values inside the struct variable.

Now, these are two structs are very similar, and we could have a lot of common functions for these two to work together. Like, lets say storing in database, or even printing the values.

But one of the major restrictions in GO, is that it is a strictly typed language and we cannot pass two different type variables to a function, even if the action performed inside the function is very similar to both the functions.

Now, below is an example, where we can use an interface to represent both the types of struct grade11Marks and grade12Marks and pass both the values and perform actions on them. However, the example below might seem very trivial, but in bigger projects where there are a lot more functions and variables, this could be a very useful method for code re-usability.

Go ahead add the following code before the struct definitions:

type marks interface {

printMarks()

}

This is a definition of an interface type named marks. What this does is basically tell GO , that all the variables that have a receiver function which is named as printMarks, then they all belong to the type marks. So basically interface is a type in GO that incorporates multiple types.

If you do not understand concept behind receiver function please read the previous article regarding such.

Note: if the receiver function returns some value, then it is to be mentioned. Like, if our function had a return type of string then instead of just printMarks() we needed to write printMarks() string.

Now, add the following function, at the end of the code:

func print(b marks) {

b.printMarks()

}

What, this function does is, takes a value b of type marks, which is an interface that incorporates all the types that have the receiver function printMarks.

And, then we call the printMarks() function to perform this action.

There could be more complex situation here, that could save us a lot of code redundancy. But for the sake of simplicity we are simply calling the printMarks() receiver function.

Now, to actually use this function, remove the following lines:

ram.printMarks()

shyam.printMarks()

And, add the following lines:

print(ram)

print(shyam)

Now, run the program and see the output.The same output as before is seen.