Interfaces in Go
Table of content
Go: Introduction to Interface
Go is an open source programming language created by Google, known for its simplicity and efficiency. One of the key features of Go is its ability to handle interfaces. Interfaces in Go allow developers to define a set of methods that a type must implement to be considered as implementing the interface. This is a powerful feature that enables developers to write generic and reusable code. In this article, we will explore interface in Go in detail.
Understanding Interfaces in Go
An interface in Go is a collection of methods that can be implemented by any type. The interface defines the behavior of the type and not its structure. This means that multiple types can implement the same interface, as long as they have the same set of methods defined in the interface. This is useful in many situations, where we want to write code that can handle multiple types in a generic way.
For example, consider a scenario where we want to write a function that can handle multiple types of shapes. We can define an interface called "Shape" that defines the methods to calculate the area and perimeter of a shape. Then, we can create different types of shapes like "Rectangle" and "Circle" that implement the Shape interface by providing their own implementation of the methods defined in the interface.
Declaring an Interface in Go
To declare an interface in Go, we use the "type" keyword followed by the name of the interface and a set of methods inside curly braces. Here is an example of declaring a Shape interface:
type Shape interface {
Area() float64
Perimeter() float64
}
Implementing an Interface in Go
To implement an interface in Go, a type must have all the methods defined in the interface. For example, to implement the Shape interface, we can create a Rectangle type as follows:
type Rectangle struct {
width, height float64
}
func (r Rectangle) Area() float64 {
return r.width * r.height
}
func (r Rectangle) Perimeter() float64 {
return 2 * (r.width + r.height)
}
Similarly, we can create a Circle type that implements the Shape interface:
type Circle struct {
radius float64
}
func (c Circle) Area() float64 {
return math.Pi * c.radius * c.radius
}
func (c Circle) Perimeter() float64 {
return 2 * math.Pi * c.radius
}
Using Interfaces in Go
Once we have defined an interface and implemented it by multiple types, we can use the interface in our code. For example, we can write a function that takes a Shape interface as an argument and calculates the area and perimeter of the shape:
func PrintShapeDetails(s Shape) {
fmt.Println("Area:", s.Area())
fmt.Println("Perimeter:", s.Perimeter())
}
We can now call the PrintShapeDetails function with instances of Rectangle and Circle types and it will work as expected:
r := Rectangle{width: 10, height: 20}
c := Circle{radius: 5}
PrintShapeDetails(r)
PrintShapeDetails(c)
Conclusion
Interfaces are a powerful feature in Go that allow developers to write generic and reusable code. They define the behavior of the type and not its structure.