dpi10335 2017-09-01 13:46
浏览 43

无法为放置在同一包中的多个结构实现接口方法

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条回答 默认 最新

  • dongyang2229 2017-09-01 13:50
    关注

    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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接