On Tue, Dec 4, 2018 at 11:57 AM Alex Dvoretskiy <advoretski...@gmail.com> wrote:
>
> Hi Golangnuts,
>
> I'm trying to implement kind of pipe function, using channels
>
> Do you think this would be a correct conception:
>
> func Pipe(fs ...task) {
>
> ch1 := make(chan interface{})
> ch2 := make(chan interface{})
>
> for _, f := range fs {
> ch1, ch2 = ch2, ch1
> go f(ch1, ch2)
> }
> }
>

It is not. You need a separate channel for each connection:

var in,out chan interface{}
for _,f:=range fs {
   out=make(chan interface{})
   go f(in,out)
   in=out
}

You don't ever use the first input channel, if you do, you need to
initialize that as well.

>
>
> Can you also explain why the function 2 and 3 don't go through the end? "f2 
> end", "f3 end" not printed :(
>
> https://play.golang.org/p/g5lL9xiM_-q


Because f2 and f3 range over a channel, and the for loops never
terminate. The for loop ends when the channel is closed. You need to
close the out channel for each f() once they're done, so the reader
would know it ended.


>
> --
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to