If I have the following interface and struct:
package shape
type Shape interface {
Area()
}
type Rectangle struct {
}
func (this *Rectangle) Area() {}
func New() Shape {
return &Rectangle{}
}
Then how can I add a method New()(as a constructor) to the interface Shape?
The use case is that if I have a different structure Square
type Square struct {
Rectangle
}
Then Squarewill have a method Area(). But he will not New(). My goal is to allow any structure that inherits Shapeto have a method New()automatically. How can i do this?
source
share