A
A
Andrey Gorbik2020-03-12 16:10:32
go
Andrey Gorbik, 2020-03-12 16:10:32

Why is an array of objects satisfying an interface not cast to an array of those interfaces?

There is a function common to several types:

type logged interface {
  GetLogID() int64
}

func maxLogID(items []logged) int64 {
  max := int64(0)
  for _, item := range items {
    if item.GetLogID() > max {
      max = item.GetLogID()
    }
  }

  return max
}


There is an object:
type A struct {
    logID int64
}

func (a A) GetLogID() int64 {
    return a.logID
}


Can't use it like this:
print(maxLogID(make([]A, 0)))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2020-03-12
@and_gorbik

Need make([]logged, 0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question