Skip to content
New issue

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

proposal: Go 2: add support for copy from a chan #34546

Closed
mei-rune opened this issue Sep 26, 2019 · 6 comments
Closed

proposal: Go 2: add support for copy from a chan #34546

mei-rune opened this issue Sep 26, 2019 · 6 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@mei-rune
Copy link

mei-rune commented Sep 26, 2019

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?

@gopherbot gopherbot added this to the Proposal milestone Sep 26, 2019
@mei-rune mei-rune changed the title proposal: Go 2 : add support for copy from chan Sep 26, 2019
@mattn
Copy link
Member

mattn commented Sep 26, 2019

If the channel is already closed, copy should panic. And I wonder this way works as atomic correctly.

@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels Sep 27, 2019
@ianlancetaylor ianlancetaylor changed the title proposal: Go 2 : add support for copy from a chan Sep 27, 2019
@ianlancetaylor
Copy link
Contributor

Does this really come up all that often?

@mei-rune
Copy link
Author

@ianlancetaylor for me, yes

@randall77
Copy link
Contributor

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?

@mei-rune
Copy link
Author

@randall77 No Generics.

I want better performance. Is that possible?
if no then close.

@ianlancetaylor
Copy link
Contributor

The performance isn't going to be any better if we build this into the runtime, so closing.

@golang golang locked and limited conversation to collaborators Sep 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
5 participants