Mastering Interfaces in Go – Polymorphism with Real Examples
What is an Interface?
An interface defines a set of method signatures. Any type that implements those methods automatically satisfies the interface — no explicit declaration needed.
type Shape interface {
Area() float64
}
type Circle struct {
Radius float64
}
func (c Circle) Area() float64 {
return 3.14 * c.Radius * c.Radius
}
Polymorphism with Interfaces
func printArea(s Shape) {
fmt.Println("Area:", s.Area())
}
c := Circle{Radius: 5}
printArea(c) // works because Circle implements Shape
The Empty Interface
interface{} (or any in newer Go versions) can hold a value of any type, similar to Object in other languages, but should be used sparingly to keep type safety.
PreviousStructs and Methods in Go – Composition over Inheritance
Next Error Handling in Go – Custom Errors, Panic, and Recover Guide
Ready to master real-world Go skills?
Learn Go hands-on with mentor-led, live sessions.
.png)