相关疑难解决方法(0)

"<type>是指向接口的指针,而不是接口"混乱

亲爱的开发者,

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

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) …
Run Code Online (Sandbox Code Playgroud)

pointers interface go

71
推荐指数
2
解决办法
4万
查看次数

隐藏零值,理解为什么golang在这里失败

我无法理解如何正确地确保nil在这种情况下不存在某些事情:

package main

type shower interface {
  getWater() []shower
}

type display struct {
  SubDisplay *display
}

func (d display) getWater() []shower {
  return []shower{display{}, d.SubDisplay}
}

func main() {
  // SubDisplay will be initialized with null
  s := display{}
  // water := []shower{nil}
  water := s.getWater()
  for _, x := range water {
    if x == nil {
      panic("everything ok, nil found")
    }

    //first iteration display{} is not nil and will
    //therefore work, on the second iteration …
Run Code Online (Sandbox Code Playgroud)

null pointers interface go

44
推荐指数
2
解决办法
1万
查看次数

标签 统计

go ×2

interface ×2

pointers ×2

null ×1