Hello,
I recently submitted a proposal to add slice expansion to channel send
statements in Go:
ch <- X... // X is a slice, array, *array, or string
The proposal was closed very quickly in the tracker before any community
discussion could take place. I would still like to get broader feedback
from Go developers, so I’m starting this discussion here.
The idea mirrors existing slice expansion in function calls (for example,
append(dst, src...)) and is exactly equivalent to:
tmpCh := ch
tmpX := X
for _, v := range (tmpX /* or *tmpX if X is *array */) {
tmpCh <- v
}
Key points:
-
ch is evaluated before X, both exactly once.
-
Sends elements in order; blocking, panic-on-closed, and partial-progress
behavior are identical to an explicit loop.
-
Nil/empty slices, zero-length arrays, and empty strings send nothing.
-
No gofmt change.
-
Proposed under GOEXPERIMENT=chanspread.
The for _, v := range xs { ch <- v } pattern is common in pipelines and
producers. This syntax makes it more concise and consistent with Go’s
existing slice expansion idiom.
Questions for you:
-
Would this improve readability/ergonomics in your code, or is the
explicit loop already optimal?
-
Does applying the existing ... mental model to sends improve language
consistency?
-
Given that iterators now exist, is this shorthand still worthwhile?
-
Any concerns that it could mislead about blocking or atomicity?
Full proposal text: https://github.com/golang/go/issues/75023
Thanks,
Ruslan.
--
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 [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/5e2a42d1-8a2d-483b-8c05-4cc0a5ab6a8cn%40googlegroups.com.