How to use Interfaces in Golang

Welcome to Golang certification class number 19—How to use interfaces in Golang programming. Today, we’ll be discussing how you can define and use interfaces in Golang programming.
 
Let’s begin by discussing why interfaces are necessary in programming.
 
An interface is can be defined as a set of methods that helps programmers to model real world objects in a simplified manner. The value of type can be used to hold any data item that implements any of the named methods and collections. We use interfaces in daily life situations.
 
For instance, if you bank your money in a fixed deposit account, you expect it to earn some interest. However, how this money makes you interest is of little concern to you. All you want at the end of interest period is your amount which includes the principal plus interest. The same concept can be applied in programming.
 
If you define an interface in Golang, the interface helps you to hide the implementation details of your objects through abstraction while only providing high level functions. For instance, high level functions could be the interest that your money accrues after some period. Interfaces also help to make an extensible and robust application based on principles of object orientation.
 
So, are you ready to learn how you can implement interfaces in Golang? In “How to use Interfaces in Golang” we learn how you can define and use interfaces in Golang.
 
Defining interfaces in Golang
Just like structs, all interfaces are defined using the key word “type” which must be followed by the interface name and the keyword “interface.” However, instead of representing the fields—as case of structs—you’’ have to the “method set” in interfaces.
 
The method set is simply a list of all your methods of a particular type that will help you to implement your interface. An interface can be viewed through the prism of object oriented programming as a class with class members that helps you to interact with data items. Here’s an example of how an interface named “myshape” can be defined in Golang:

 type myshape interface {
  area() float32
}

In the above example, myshape is the name of the interface that we’ve created. This interface has one method called, “area ()” which is of type float32.
 
Once you’ve defined the interface methods, you should go ahead and provide the implementation of these methods. For instance, the method area () can be implemented as follows:

type Rect struct { 
length, width int
}
func (a Rect) area() float32 {
return a.length * a.width
}

The first section of the code creates a “Rect” struct that should implement the area () method of the interface we defined earlier. The second code is a function that implements how the area of a given rectangle should be computed.
 
In a nutshell, here’s what we have defined:

    • The name of the interface is myshape.
    • It has one method called area ().
    • There’s one shape implementation that has been defined called rectangle.
    • To calculate the area of the rectangle we use the formula: return a.length * a.width.

So, how can you implement such an interface in Golang?
 
Working with interfaces in Golang
To apply interfaces in Golang, we use the same concept we learnt in structs. For instance, we’ll use the “.” operator to access fields in a particular object. Here’s an example of how the above interface definition can be implemented in Golang:

package main
import "fmt"
type myshape interface {
  area() float32
}
type Rect struct { 
length, width int32
}
func (a Rect) area() float32 {
return a.length * a.width
}
func main () {
r := Rect{length:10.4, width:5.3}
fmt.Println ("Rectangle r's area is: ", r.area())  
b: = myshape(r)
fmt.Println ("The Area of the myshape r is: ", b.area())  
}

There you have it—start defining and implementing interfaces in Golang. Remember, we’re here to help you. Get in touch if you have queries that you’d like us to respond to.

Online Technology Courses Stone River eLearning

GET YOUR FREE Golang EBOOK!

Comments