douhuan1648
2016-02-26 13:23 阅读 54
已采纳

Golang:通过空接口传递频道

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    du22399 du22399 2016-02-26 13:54

    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.

    点赞 评论 复制链接分享

相关推荐