相关疑难解决方法(0)

如何使用相同的方法签名实现两个不同的接口

假设我必须实现在两个不同的包中声明的两个不同的接口(在两个不同的分离项目中).

我有包裹 A

package A

type interface Doer {
    Do() string
}

func FuncA(Doer doer) {
     // Do some logic here using doer.Do() result

     // The Doer interface that doer should implement, 
     // is the A.Doer
}
Run Code Online (Sandbox Code Playgroud)

并在包中 B

package B

type interface Doer {
    Do() string
}

function FuncB(Doer doer) {
    // some logic using doer.Do() result

     // The Doer interface that doer should implement, 
     // is the B.Doer
}
Run Code Online (Sandbox Code Playgroud)

在我的main包裹中

package main

import (
    "path/to/A"
    "path/to/B" …
Run Code Online (Sandbox Code Playgroud)

interface go

7
推荐指数
1
解决办法
2747
查看次数

标签 统计

go ×1

interface ×1