Interfaces in Golang


Interfaces in Golang are a powerful feature that allows developers to define a set of methods that must be implemented by any type that satisfies that interface. Interfaces help to make code more modular, reusable, and easier to maintain. In this article, we will explore interfaces in Golang, how to create them, and how to use them effectively.

What are Interfaces in Golang?

An interface in Golang is a collection of method signatures that define a set of behaviors that a type must support to satisfy that interface. In other words, an interface defines what methods a type must have but does not specify how those methods should be implemented. An interface is like a contract that specifies the behavior that a type must support.

Creating Interfaces in Golang

To create an interface in Golang, you need to use the "type" keyword followed by the name of the interface and the set of method signatures. Here is an example of how to create an interface in Golang −

type Shape interface {
   Area() float64
   Perimeter() float64
}

This interface defines two methods, Area() and Perimeter(), that must be implemented by any type that satisfies this interface. Note that we do not provide any implementation details in the interface definition.

Using Interfaces in Golang

Once you have defined an interface in Golang, you can use it to define functions, methods, and variables that can accept any type that satisfies that interface. Here is an example of how to use the Shape interface defined above −

Example

package main

import "fmt"

type Shape interface {
   Area() float64
   Perimeter() float64
}

type Rectangle struct {
   width  float64
   height float64
}

func (r Rectangle) Area() float64 {
   return r.width * r.height
}

func (r Rectangle) Perimeter() float64 {
   return 2*r.width + 2*r.height
}

type Circle struct {
   radius float64
}

func (c Circle) Area() float64 {
   return 3.14 * c.radius * c.radius
}

func (c Circle) Perimeter() float64 {
   return 2 * 3.14 * c.radius
}

func printShapeDetails(s Shape) {
   fmt.Println("Area:", s.Area())
   fmt.Println("Perimeter:", s.Perimeter())
}

func main() {
   r := Rectangle{10, 20}
   printShapeDetails(r)
   
   c := Circle{5}
   printShapeDetails(c)
}

Output

Area: 200
Perimeter: 60
Area: 78.5
Perimeter: 31.400000000000002

In this example, we define a function called printShapeDetails that takes a Shape interface as an argument. The function can be called with any type that satisfies the Shape interface, which means that it can be called with both Rectangle and Circle types.

Benefits of Using Interfaces in Golang

  • Modularity − Interfaces help to make code more modular and easier to maintain by separating the interface from the implementation details.

  • Reusability − Interfaces allow you to write code that can work with any type that satisfies that interface, which means that you can reuse the same code for multiple types.

  • Testability − Interfaces make it easier to write testable code by allowing you to create mock implementations of the interface for testing purposes.

Conclusion

Interfaces are a powerful feature in Golang that can help to make code more modular, reusable, and easier to maintain. By defining a set of method signatures that a type must support, interfaces allow you to write code that can work with any type that satisfies that interface. When used effectively, interfaces can help to make your code more flexible and adaptable to changing requirements.

Updated on: 25-Apr-2023

295 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements