小编典典

“ 是接口的指针,而不是接口的混乱

go

亲爱的开发人员,

我遇到了这个问题,对我来说似乎有点奇怪。看一下这段代码:

package coreinterfaces

type FilterInterface interface {
    Filter(s *string) bool
}

type FieldFilter struct {
    Key string
    Val string
}

func (ff *FieldFilter) Filter(s *string) bool {
    // Some code
}

type FilterMapInterface interface {
    AddFilter(f *FilterInterface) uuid.UUID     
    RemoveFilter(i uuid.UUID)                   
    GetFilterByID(i uuid.UUID) *FilterInterface
}

type FilterMap struct {
    mutex   sync.Mutex
    Filters map[uuid.UUID]FilterInterface
}

func (fp *FilterMap) AddFilter(f *FilterInterface) uuid.UUID {
    // Some code
}

func (fp *FilterMap) RemoveFilter(i uuid.UUID) {
    // Some code
}

func (fp *FilterMap) GetFilterByID(i uuid.UUID) *FilterInterface {
    // Some code
}

在其他包装上,我有以下代码:

func DoFilter() {
    fieldfilter := &coreinterfaces.FieldFilter{Key: "app", Val: "152511"}
    filtermap := &coreinterfaces.FilterMap{}
    _ = filtermap.AddFilter(fieldfilter) // <--- Exception is raised here
}

运行时不接受提到的行,因为

“不能在fieldint.AddFilter的参数中使用fieldfilter(类型为
coreinterfaces.FieldFilter)作为类型为
coreinterfaces.FilterInterface:*
coreinterfaces.FilterInterface是指向接口而不是接口的指针”

但是,将代码更改为:

func DoBid() error {
    bs := string(b)
    var ifilterfield coreinterfaces.FilterInterface
    fieldfilter := &coreinterfaces.FieldFilter{Key: "app", Val: "152511"}
    ifilterfield = fieldfilter
    filtermap := &coreinterfaces.FilterMap{}
    _ = filtermap.AddFilter(&ifilterfield)
}

一切都很好,并且在调试应用程序时,它似乎确实包括

我对此话题有些困惑。当看着其他博客和堆栈溢出线程讨论这个完全一样的问题(例如-
,)这引起了这个异常应该工作,因为这两个fieldfilter和fieldmap初始化为指针接口,而不是价值的第一个片段接口。为了避免不声明FieldInterface并为该接口分配实现,我一直无法解决这里实际发生的事情。必须有一种优雅的方法来做到这一点。


阅读 437

收藏
2020-07-02

共1个答案

小编典典

因此,您在这里混淆了两个概念。指向结构的指针和指向接口的指针是不同的。接口可以直接存储结构 指向结构的指针。在后一种情况下,您仍然仅直接使用该接口,
而不是 指向该接口的指针。例如:

type Fooer interface {
    Dummy()
}

type Foo struct{}

func (f Foo) Dummy() {}

func main() {
    var f1 Foo
    var f2 *Foo = &Foo{}

    DoFoo(f1)
    DoFoo(f2)
}

func DoFoo(f Fooer) {
    fmt.Printf("[%T] %+v\n", f, f)
}

输出:

[main.Foo] {}
[*main.Foo] &{}

https://play.golang.org/p/I7H_pv5H3Xl

在这两种情况下,fin中的变量DoFoo都只是一个接口, _而不是_指向接口的指针。但是,在存储时f2,接口 _保留_指向Foo结构的指针。

指向接口的指针几乎 _永远不会_有用。实际上,Go运行时专门修改了几个版本,使其不再自动取消对接口指针的引用(就像对结构指针一样),以阻止使用它们。在绝大多数情况下,指向接口的指针反映了对接口应该如何工作的误解。

但是,接口上有限制。如果将结构直接传递到接口,则只能使用该类型的 方法(即func (f Foo) Dummy()not func (f*Foo)Dummy())来实现接口。这是因为您在接口中存储了原始结构的副本,因此指针方法会产生意想不到的效果(即无法更改原始结构)。因此,默认的经验法则是在接口中存储指向结构的指针 ,除非有令人信服的理由不这样做。

如果将AddFilter函数签名更改为:

func (fp *FilterMap) AddFilter(f FilterInterface) uuid.UUID

和GetFilterByID签名可以:

func (fp *FilterMap) GetFilterByID(i uuid.UUID) FilterInterface

您的代码将按预期工作。 fieldfilter是type
*FieldFilter,它会填满FilterInterface接口类型,因此AddFilter将接受它。

这里有一些很好的参考,用于理解Go中方法,类型和接口如何工作以及如何相互集成:

2020-07-02