Constructor method in interface? (in the Golang)

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?

+4
source share
2 answers

Go does not have the ability to create methods on interfaces.

, , . Shape, New , :

func New(s Shape) Shape { ... }

, , .

: http://play.golang.org/p/NMlftCJ6oK

+6

, . . - , .

0

All Articles