Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang: Passing channels through empty interfaces

Tags:

go

I'm trying to do something that seems like it should be trivial until I read up and now it seems like it should be really complex. ;-) I've knocked up a test pattern to illustrate: http://play.golang.org/p/Re88vJZvPT

At the most basic I'm trying to have a function that can read data from a channel and spit it out on another one. Easy. The test does this as long as you use the pusher function shown. However the problem with this is that doing it this way I would need a different pusher function for each type of data I want to push through it. Now I've done similar things in the past with an empty interface as nothing in the pusher code cares about what's in the data structure. What I can't figure out is when I'm dealing with channels of an un-cared-about data structure.

To illustrate the concept of what I'm trying to achieve please see the function pusher_naive_generic.

However that doesn't work either so more reading up implied the way to do it was making use of reflection and finally you see my pusher_reflect_generic function(obviously this won't achieve the same intended function as the others it's showing where I got to before getting stuck). Which still fails because I can't get from an interface that contains a chan to the structure read from that chan.

Hopefully the code makes more sense of what I'm trying to achieve than my words actually do. I can make all of this work by explicitly coding for every type, but what I can't figure out how to do is code it for any future type.

like image 322
Chris Hopkins Avatar asked Jan 31 '26 11:01

Chris Hopkins


1 Answers

If I have understood your question correctly, then this might be the solution:

http://play.golang.org/p/xiDO7xkoW4

func forwardChannel(i interface{}, o interface{}) {

    it, ot := reflect.TypeOf(i), reflect.TypeOf(o)

    if it != ot {
        panic("forwardChannel: both arguments must be of the same type")
    }

    iv, ov := reflect.ValueOf(i), reflect.ValueOf(o)

    for {
        v, k := iv.Recv()
        if !k {
            break
        }
        ov.Send(v)
    }

    ov.Close()

}

Note that Recv, Send and Close panic if i and o are not channels.

like image 108
thwd Avatar answered Feb 03 '26 05:02

thwd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!