Convert chan of something to chan of interface{}

2,537 views
Skip to first unread message

makkalot

unread,
Sep 17, 2014, 9:28:14 AM9/17/14
to golan...@googlegroups.com
Hi,

I have that function with signature like : func MergeChannelsGeneric(cs
[]<-chan interface{}) <-chan interface{}
and then want to write the type specific version which calls the one
above : func MergeWorkerChans(workers map[string]Worker) <-chan [][]byte

I couldn't find a way to convert []<-chan [][]byte to [] <-chan
interface{} and also the reverse operation is not possible : <-chan
interface{} to <-chan [][]byte
Is there a way to accomplish this ?

Here is a snippet of what i'm doing :

workerChans := make([]<-chan [][]byte, len(workers))
genericChans := make([]<-chan interface{}, len(workers))

i := 0
for _, w := range workers {
genericChans[i] = w.GetOutChan()
i = i + 1
}

I'm getting : cannot use w.GetOutChan() (type <-chan [][]byte) as type
<-chan interface {} in assignment

Thanks in advance.

Ian Lance Taylor

unread,
Sep 17, 2014, 9:41:28 AM9/17/14
to makkalot, golang-nuts
On Wed, Sep 17, 2014 at 6:27 AM, makkalot <makkal...@gmail.com> wrote:
>
> I have that function with signature like : func MergeChannelsGeneric(cs
> []<-chan interface{}) <-chan interface{}
> and then want to write the type specific version which calls the one above :
> func MergeWorkerChans(workers map[string]Worker) <-chan [][]byte
>
> I couldn't find a way to convert []<-chan [][]byte to [] <-chan interface{}
> and also the reverse operation is not possible : <-chan interface{} to
> <-chan [][]byte
> Is there a way to accomplish this ?

No. Sorry.

To do anything along these lines you have to work with values of type
interface{} and use the reflect package.

Ian

makkalot

unread,
Sep 17, 2014, 11:28:55 AM9/17/14
to Ian Lance Taylor, golang-nuts
Thanks for your answer. How do you write generic code that includes
channels then ?
It is possible to do the same with other types, like writing the generic
version (with interface{})
and then writing the specialized version which does the conversion from
interface to/from your type.
Something like merging a slice of channels into a single channel seems
something like reusable to me,
at least i use it very often. Probably there are some other reusable
patterns too. What is the way to do
that, copy/paste ?

Thanks.

Ian Lance Taylor

unread,
Sep 17, 2014, 11:43:11 AM9/17/14
to makkalot, golang-nuts
On Wed, Sep 17, 2014 at 8:27 AM, makkalot <makkal...@gmail.com> wrote:
> Thanks for your answer. How do you write generic code that includes channels
> then ?
> It is possible to do the same with other types, like writing the generic
> version (with interface{})
> and then writing the specialized version which does the conversion from
> interface to/from your type.
> Something like merging a slice of channels into a single channel seems
> something like reusable to me,
> at least i use it very often. Probably there are some other reusable
> patterns too. What is the way to do
> that, copy/paste ?

More or less, yes.

http://golang.org/doc/faq#generics

Ian

rjeczalik

unread,
Sep 17, 2014, 2:31:21 PM9/17/14
to makkalot, golang-nuts
On 17 September 2014 17:27, makkalot <makkal...@gmail.com> wrote:
> Something like merging a slice of channels into a single channel seems something like reusable to me,
> at least i use it very often.

You can always use reflect, e.g. http://play.golang.org/p/24wHyuQvtV, but I'm not sure it's worth it, as reflect.Select is not free.

makkalot

unread,
Sep 17, 2014, 3:52:26 PM9/17/14
to rjeczalik, golang-nuts
Thanks.
Reply all
Reply to author
Forward
0 new messages