0

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?

1

1 Answer 1

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.