Unable to work with interface imported from another pkg, says method is missing but its there

huangapple go评论128阅读模式
英文:

Unable to work with interface imported from another pkg, says method is missing but its there

问题

我无法使用从另一个包导入的接口,并且不确定该怎么做。示例代码:

var inner types.TxData // <-- 从 types 包导入的接口
inner = &InternalTx{
    ChainId: (*big.Int)(dec.ChainID),
    Data:    *dec.Data,
}

在接口中列出的所有方法中,有一个方法不被接受:

func (tx *InternalTx) accessList() types.AccessList { return nil }

Go 抱怨 InternalTx 没有实现 accessList() 方法以满足 types.TxData 接口,但是如果我将 accessList() 改为 Accesslist(),则会得到另一个错误提示:

have AccessList() types.AccessList
want accessList() types.AccessList

所以我非常困惑我应该在这里做什么?

编辑:

根据最近的建议,我还实现了以下内容:

type TxData struct {
    types.TxData
}
var inner TxData

internalTx := &InternalTx{
    ChainId: (*big.Int)(dec.ChainID),
    Data:    *dec.Data,
}
inner = TxData{TxData: internalTx}

问题仍然存在。

英文:

I'm unable to work with an interface imported from another package and not sure what I should do. Example code:

var inner types.TxData // &lt;-- interface imported from types package
inner = &amp;InternalTx{
		ChainId: (*big.Int)(dec.ChainID),
		Data:    *dec.Data,
	}

Out of all the methods listed in the interface, 1 method is not accepted:
func (tx *InternalTx) accessList() types.AccessList { return nil }

Go complains that InternalTx does not implement accessList() to satisfy types.TxData interface, but if i capitalise accessList() to Accesslist() then I get another complaint stating that :

have AccessList() types.AccessList
want accessList() types.AccessList

So i'm very confused what I need to do here?

Edit:

I've implemented also the following based on recent suggestion:

type TxData struct {
	types.TxData
}
var inner TxData

internalTx := &amp;InternalTx{
ChainId: (*big.Int)(dec.ChainID),
        Data:    *dec.Data,
}
inner = TxData{TxData: internalTx}

Issue still persists.

答案1

得分: 1

如果一个接口声明了一个未导出的方法(比如accessList),那么你不能在另一个包中实现该接口,因为该名称未被导出。这是一种强制你使用原始包中的实现的常见方式。

处理这个问题的一种方法是嵌入该接口,然后将功能委托给它的实现:

type TxData struct {
   types.TxData
}

someImplementation := // 获取 types.TxData 的一个实现
inner := TxData{TxData: someImplementation}

这将使用someImplementationaccessList方法。

英文:

If an interface is declared with an unexported method (such as accessList), then you cannot implement that interface in another package, because that name is not exported. This is a common way to force you to use implementations in the original package.

One way to deal with this is to embed that interface, and then delegate functionality to an implementation of it:

type TxData struct {
   types.TxData
}

someImplementation:=// Get an implementation of types.TxData
inner:=TxData{TxData: someImplementation}

This will use the accessList of someImplementation.

huangapple
  • 本文由 发表于 2022年9月2日 09:23:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/73576903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定