In Go, when a type implements an interface, it must implement all the methods defined in that interface. Your geometry
interface specifies two methods: area()
and perim()
. However, the circle
type only implements the area()
method—it does not implement perim()
.
So when you call detectShape(cir1)
, Go expects cir1
to fully satisfy the geometry
interface, but it doesn't, because it lacks the perim()
method. This results in a compilation error.
Your first call (fmt.Print(cir1.area(), " ")
) works just fine because you're directly calling the method implemented in circle
, without involving the interface.
To fix this issue, you need to provide an implementation for perim()
in circle
, like so :
func (c circle) perim() float32 {
return 2 * math.Pi * c.radius
}
Once you add this, circle
will fully satisfy geometry
, and detectShape(cir1)
will execute properly.