I'm attempting to generalize some of my code and I thought I might be able to pull some common code together, but I'm hitting my head against a type-system problem.
Let's assume I have an interface like this:
type Hashable interface {
GetHash() []byte
}
and I have a couple of concrete types like this:
type Transaction struct {
Hash []byte `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"`
}
func (m *Transaction) GetHash() []byte {
if m != nil {
return m.Hash
}
return nil
}
Notice that these are generated by protoc
, and I can probably not change them easily. While the type itself does not implement Hashable
, the pointer to the struct should if I'm not mistaken.
The issue arises when I create a function that accepts a channel of type chan Hashable
and try to pass in a chan *Transaction
, like this:
func consume(c chan Hashable) {
// Do something with the elements from c
}
func main() {
var c2 chan *Transaction
consume(c2)
}
(I created a small example to show this issue in the Playground)
I could of course create a new struct that repacks the fields that consume
needs, but that seems like a much worse option.
Is there a clean solution for this?