You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
That is how the language works. The fact that you can assign chan int to <-chan int doesn't mean that you can assign []chan int to []<-chan int. The language rules are documented at https://golang.org/ref/spec#Assignability .
Thanks for your comment, how could I achieve this ? By recreating another slice of channels in receive-only mode ? Could go infer the type here to avoid that ?
@scorsi I found this issue as I have exactly the same problem. The only way around it that I found was to create a new slice and copy the elements from the []chan int to the []<-chan int one at a time, in a loop. Of course, this is ugly, slow (for a slice of millions of chans) and add a lot of unnecessary strain on the GC. I suggest you make a language change proposal.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Check it out on play.golang.org
The code is as simple as that:
What did you expect to see?
When we don't use a slice, returning a receive-only channel works. Example:
Why it'sn't the case for slice of chan ?
What did you see instead?
A compile error.
The text was updated successfully, but these errors were encountered: