I have three files:
node.go:
type Node interface {
AMethod(arg ArgType) bool
BMethod() bool
}
anode.go:
type aNode struct {}
func AMethod(aNode ANode, arg ArgType) bool {
return true
}
func BMethod(aNode ANode) bool {
return true
}
bnode.go:
type bNode struct {}
func AMethod(bNode BNode, arg ArgType) bool {
return true
}
func BMethod(bNode BNode) bool {
return true
}
But I'm getting the error:
Nodes/bnode.go:16:58: AMethod redeclared in this block
previous declaration at Nodes/anode.go:15:58
Nodes/bnode.go:20:60: BMethod redeclared in this block
previous declaration at Nodes/anode.go:19:60
How do I validly implement an interface here?
Declaring a function that accepts a certain type does not make that function part of the type's method set (meaning it does not help the type satisfy a particular interface).
Instead, you need to use the proper syntax to declare a function as a method, like so:
type BNode struct {}
func (ANode) AMethod(arg ArgType) bool {
return true
}
func (ANode) BMethod() bool {
return true
}
type BNode struct {}
func (BNode) AMethod(arg ArgType) bool {
return true
}
func (BNode) BMethod() bool {
return true
}