Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
extend copy() function. copy from a buffered chan likes
copy([]T, <- chan T) : int
equal to
func(dst []T, src <- chan T) : int { for idx := 0; idx < len(dst); idx ++ { select { case a, ok := <- src: if !ok { return idx } dst[idx] = a default: return idx } } return len(dst) }
examples:
c := make(chan int, 100) c <- 1 c <- 2 c <- 3 ... a := make([]int, 10) n := copy(a, c) a = a[:n]
My main intent is to make it fast for receive message from chan. Is that possible?
The text was updated successfully, but these errors were encountered:
If the channel is already closed, copy should panic. And I wonder this way works as atomic correctly.
Does this really come up all that often?
@ianlancetaylor for me, yes
What's wrong with using the 14-line function you showed? Is it just lack of generics? How many different types are you using this for?
@randall77 No Generics.
I want better performance. Is that possible? if no then close.
The performance isn't going to be any better if we build this into the runtime, so closing.
extend copy() function. copy from a buffered chan likes
copy([]T, <- chan T) : int
equal to
examples:
My main intent is to make it fast for receive message from chan. Is that possible?
The text was updated successfully, but these errors were encountered: