Polymorphism is a fundamental concept in object-oriented programming that allows objects of different types to be treated uniformly. In Go, polymorphism is achieved through interfaces, enabling flexible and extensible code design.
Go implements polymorphism using interfaces. An interface defines a set of method signatures, and any type that implements all these methods is said to satisfy the interface. This approach allows for runtime polymorphism without the need for explicit inheritance.
Let's explore a simple example to demonstrate polymorphism in Go:
package main
import (
"fmt"
)
// Shape interface
type Shape interface {
Area() float64
}
// Rectangle struct
type Rectangle struct {
Width float64
Height float64
}
// Circle struct
type Circle struct {
Radius float64
}
// Area method for Rectangle
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
// Area method for Circle
func (c Circle) Area() float64 {
return 3.14 * c.Radius * c.Radius
}
func main() {
shapes := []Shape{
Rectangle{Width: 3, Height: 4},
Circle{Radius: 5},
}
for _, shape := range shapes {
fmt.Printf("Area: %f\n", shape.Area())
}
}
In this example, both Rectangle
and Circle
implement the Shape
interface by providing an Area()
method. This allows us to treat them polymorphically in the main()
function.
Polymorphism in Go, implemented through interfaces, provides a powerful tool for creating flexible and maintainable code. By understanding and applying this concept effectively, developers can write more modular and extensible Go programs.
To deepen your understanding of Go's type system and related concepts, explore Go Interfaces and Go Methods. These topics will provide a solid foundation for mastering polymorphism in Go.