go - how can I declare a slice of chan (channels) in func -
I am trying to write a function like this, but I can not declare the piece of the channels < Pre> Is the fragment of the channel as a logic? Example of the call If I can do this I think it should be possible Updated: I have set a bit syntax for your function, it now compiles: Edit: There was a problem using the func fanIn (set & lt; - [] chan string) & lt; -chan string {c: for = make (chan string) i: = range set {go func () {for {c & lt; -set [i]}}} ()} Return C}
Set: = [2] chan string {mylib.Boring ("which"), mylib.Boring ("n"}} c: = FanIn (set)
func fanIn (input1, input2 & lt; -chan string) & lt; -chan string {
Funny fan in (set [] & lt; -chan string) & Lt; -chan string {i: = class set {go for func ()} {x: = & lt; -set [i] c & lt; - x}} ()} Return C} func main () {Set: = [] & lt; -chan string {mylib.Boring ("joe"), mylib.Boring ("n"), mylib.bo ring ("max")} c: = fanIn (set) i: = 0; I & lt; 10; I ++ {fmt.Println (& lt; -c)} fmt.Println ("You're boring: I'm going.")}
func fanIn (set [] & Lt; -chan string) & lt; -chan string {c: i = = (chain string) for set = // Here is the main change - you get from one channel and send it to one. // The way you wrote, you are sending the channel to another channel: func () {for {c & lt; - & lt; BTW for readability, I will write it to:
go func () {for { X: = & lt; -set [i] c & lt; - x}} ()
set [i] inside the goroutine in your original code, making them all the last Can be read from the channel Here's a definite version:
func fanIn (set [] & lt; -chan string) & lt; -chan string {c: for = make (chan string) i: = range set {go func (& lt; -chan in string) {for {x: = & lt; - c & lt; -x}} (set [i]) returning c}
Comments
Post a Comment