What is the simplest way to add all the elements of an array to a channel?
I can do this:
elms := [3]int{1, 2, 3}
c := make(chan int, 3)
for _, e := range elms {
c <- e
}
But I wonder if there is a syntactic sugar for this.
What is the simplest way to add all the elements of an array to a channel?
I can do this:
elms := [3]int{1, 2, 3}
c := make(chan int, 3)
for _, e := range elms {
c <- e
}
But I wonder if there is a syntactic sugar for this.
Code should be correct, maintainable, robust, reasonably efficient, and, most importantly, readable.
By design, Go is simple, but powerful. Everybody can read and memorize the specification: The Go Programming Language Specification. You can learn Go in a day or so. The simplicity makes Go code very readable.
The complexity of syntactic sugar induces cognitive overload. After working alongside Bjarne Stroustrup (C++) and Guido van Rossum (Python), the Go authors deliberately avoided syntactic sugar.
Read Bjarne Stroustrup's recent lament about the complexity of C++: Remember the Vasa!.
It's easy to see what this code does:
package main
func main() {
elms := [3]int{1, 2, 3}
c := make(chan int, len(elms))
for _, elm := range elms {
c <- elm
}
}