I am trying to understand how interfaces work in Go.
Let's say I have 2 structs:
package "Shape"
type Square struct {
edgesCount int
}
type Triangle struct {
edgesCount int
}
Now I create a Shape
interface:
type Shape interface {
}
Why can't I specify that the Shape
interface has an egdesCount
property? Are interfaces only supposed to regroup methods?
Another problem I face is sharing function. Isn't possible to come up with something like this:
func New() *Shape {
s:=new(Shape)
s.edgesCount = 0
return s
}
This would be much better than having to rewrite the exact same code:
func New() *Square {
s:=new(Square)
s.edgesCount = 0
return s
}
func New() *Triangle {
s:=new(Triangle)
s.edgesCount = 0
return s
}
(which also poses problem as I cannot redeclare my New
function...)
Many thanks for your help