Look at this code. Everything works fine:
type moninterface2 interface {
methode_commune(a,b int) int
}
type classe1 struct {
a, b int
}
type classe2 struct {
c, d int
}
func (r classe1) methode_commune(a,b int) int {
return a+b
}
func (r classe2) methode_commune(a,b int) int {
return a*b
}
func fonctiontest(param moninterface2) {
ret := param.methode_commune(2,3)
fmt.Println(ret)
}
But if I declare methode_commune
like this:
func (r *classe1) methode_commune(a,b int) int
func (r *classe2) methode_commune(a,b int) int
Go does not consider classe1
and classe2
implements moninterface2
and the code does not compile. I do not understand why.