I don't understand why the following code does not compile.
I am confused why Go is saying HistoryReader
does not correctly implement IReader
. HistoryBook
implements IBook
. Why are Read(book Ibook)
and Read(book HistoryBook)
not acceptable together when trying to add a HistoryReader
to a slice of IReader
s?
package main
type IReader interface {
Read(book IBook)
}
// HistoryReader implements IReader
type HistoryReader struct{}
func (r *HistoryReader) Read(book HistoryBook) {
// ...
}
type IBook interface{}
// HistoryBook implements IBook
type HistoryBook struct{}
func main() {
var readerSlice []IReader
_ = append(readerSlice, &HistoryReader{})
}
./main.go:28:26: cannot use &HistoryReader{} (value of type *HistoryReader) as type ReaderInterface in argument to append:
*HistoryReader does not implement ReaderInterface (wrong type for Read method)
have Read(book HistoryBook)
want Read(book BookInterface)