NB: This is not a duplicate of this question, because I understand when you would use one-directional channels. I do so all the time. My question is why this program is valid:
func main() {
    ch := make(chan<- int)
    ch <- 5
    fmt.Println("Hello, playground")
}
Playground
Running it, of course, gives a deadlock. If you check the type with %T, Go does in fact report that the type of ch is a send-only channel. In Go, you're allowed to make uni-directional channels, but it makes little sense because by making a channel that at its inception is one-way only, you're ensuring at least one of read/write will never occur.
A possible explanation would be to force a goroutine to hang but that's just as easily accomplished with select {}. 
My only other idea is to force a goroutine to only do something n times,
ch := make(chan<- int, 50)
// do something 50 times, since then the buffer is full
for {
  ch <- doSomething()
}
But that's more easily, not to mention less confusingly, accomplished with any number of different constructions.
Is this just a quirk/oversight of the type system, or is there use for this behavior I'm not thinking of?
You have a language: words (tokens) and a grammar. You can always write valid nonsense: blue is not a color.
You wrote some valid nonsense:
package main
func main() {
    ch := make(chan<- int)
    ch <- 5
}
Here's some more valid nonsense:
package main
func main() {
    for {
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With