Interfaces are defined by their functionality and not by the data they contain. This, I find, makes mimicking C style inheritance difficult for Plain Old Data (POD) classes. The only solution I can think of is to implement a method that does nothing for all struct that implement the interface. Consider the below example with "fooSignatureMove"
package main
type foo interface{
// fooSignatureMove does nothing but allow to mimick inheritence
fooSignatureMove()
}
type A struct{}
type B struct{}
type C struct{}
func (*A) fooSignatureMove(){}
func (*B) fooSignatureMove(){}
func main(){
arr := make([]foo, 2)
arr[0] = &A{}
arr[1] = &B{}
arr[2] = &C{} // I do not want this to compile
}
Is this good practice?
CodePudding user response:
As @mkopriva explained in a comment, this pattern is indeed quite common in the standard library, so it is probably the way to go. See for example ast.Expr and exprNode