golang 这里有人用吗
居然没有内置这种功能。。。
想到个办法,在此抛砖引玉
刚试着弄了个,思路是制造一个链式的结构,
链上的每个元素对应一个 goroutine ,这个 goroutine
读取上一个节点发过来的数据扔到输出 channel 上,并转发给下一个。
输出 channel 阻塞就退出自己。(所以发送必须比接收慢)
而且链上有多少个节点,相当于多少容量的 buffer ,
无法确认消息送达到全部 channel
type chaincast struct {
v interface{} //message payload.
out chan interface{} //message output channel
spawn chan<- chaincast //send to this channel to spawn goroutine for chain element.
next chan chaincast //channel for passing message to next
prev chan chaincast //channel for receiving message from prev
}
代码在这,可能有大 bug :
https://play.golang.org/p/6D9wZ9Qnp8
居然没有内置这种功能。。。
想到个办法,在此抛砖引玉
刚试着弄了个,思路是制造一个链式的结构,
链上的每个元素对应一个 goroutine ,这个 goroutine
读取上一个节点发过来的数据扔到输出 channel 上,并转发给下一个。
输出 channel 阻塞就退出自己。(所以发送必须比接收慢)
而且链上有多少个节点,相当于多少容量的 buffer ,
无法确认消息送达到全部 channel
type chaincast struct {
v interface{} //message payload.
out chan interface{} //message output channel
spawn chan<- chaincast //send to this channel to spawn goroutine for chain element.
next chan chaincast //channel for passing message to next
prev chan chaincast //channel for receiving message from prev
}
代码在这,可能有大 bug :
https://play.golang.org/p/6D9wZ9Qnp8