Golang:我可以投射到chan界面{}

I am trying to write a general purpose wrapper for subscriptions, something like:

type Subscriber interface{
    Subscribe(addr string) chan interface{}
}

Suppose there is a library I want to use which has a subscribe method in it, but which uses a chan library.Object. I would like to be able to do something like:

func (s *mySubscriber) Subscribe(addr string) chan interface{}{
    ch := make(chan library.Object)
    library.Subscribe(addr, ch)
    return chan interface{}(ch)
}

Currently, I don't believe such a cast is possible. And I don't want to modify the underlying library, since the wrapper should be agnostic to library implementations.

I've seen Is there a way to cast Structs for sending over a channel, but in that case the application can be modified to suit the need. Here, it can't. Is this possible? Is there a better way?

One solution is to pass in a general purpose channel into Subscribe, and to wait indefinetely on chan library.Object and fire anything that comes through on my general channel, but I didn't particularly like having to introduce another channel just to get around the type cast.

No, you can't do this with just a cast. You have to use an extra channel, as you have already considered. Fortunately, there is a helper library for this already (disclaimer: I wrote it). You want the Wrap function.

For anyone else that stumbles on this issue and wants some inline code:

// wrap a timeout channel in a generic interface channel
func makeDefaultTimeoutChan() <-chan interface{} {
  channel := make(chan interface{})
  go func() {
    <-time.After(30 * time.Second)
    channel <- struct{}{}
  }()
  return channel
}

// usage
func main() {
  resultChannel := doOtherThingReturningAsync()
  cancel := makeDefaultTimeoutChan()
  select {
    case <-cancel:
      fmt.Println("cancelled!")
    case results := <-resultChannel:
      fmt.Printf("got result: %#v
", results)
  }
}