6

In Go one can create one-way channels. It's a very convenient feature in case of one want to restrict a set of operations available on the given channel. However, as far as I can see, this feature is useful only for function's arguments and variable's type specification, while creating one-way channels via make looks strange for me. I've read this question, but it's not about creating read (or write)-only channels in Go, it's about usage in general. So, my question is about use cases of the next code:

writeOnly := make(chan<- string)
readOnly := make(<-chan string)
Community
  • 1
  • 1
Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
  • There is no point really; don't make directional channels. – JimB Apr 22 '16 at 16:11
  • @JimB, could you explain why, please? – Ivan Velichko Apr 22 '16 at 16:25
  • You can't use a directional-only channel for anything useful, other than blocking forever, so there's no need to ever create one. – JimB Apr 22 '16 at 16:35
  • @JimB, ok, it's about creation. But as far as I can see, in general, there are justified use cases of unidirectional channels. For example, in function signature, is not it? – Ivan Velichko Apr 22 '16 at 16:38
  • 3
    yes, directional channels are used all the time, you just don't `make` them that way. – JimB Apr 22 '16 at 16:44
  • There's another question identical to this one, but has much better answers. Hint: you can create bidirectional channels and convert them on the fly to write-only or read-only per your needs. Take a look on this SO [question](https://stackoverflow.com/questions/13596186/whats-the-point-of-one-way-channels-in-go) for more details. – eAbi Oct 12 '17 at 15:41

2 Answers2

4

Theoretically you can use write only channels for unit testing to ensure for example that your code is not writing more than specific number of times to a channel.

Something like this: http://play.golang.org/p/_TPtvBa1OQ

package main

import (
    "fmt"
)

func MyCode(someChannel chan<- string) {
    someChannel <- "test1"
    fmt.Println("1")
    someChannel <- "test2"
    fmt.Println("2")
    someChannel <- "test3"
    fmt.Println("3")
}

func main() {
    writeOnly := make(chan<- string, 2) // Make sure the code is writing to channel jsut 2 times
    MyCode(writeOnly)
}

But that would be pretty silly technique for unit testing. You're better to create a buffered channel and check its contents.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Alexander Trakhimenok
  • 6,019
  • 2
  • 27
  • 52
0

One of the main reason that people use types (especially in Go) is as a form of documentation. Being able to show that a channel is read-only or write-only, can help the consumer of the API have a better idea of what is going on.

matt.s
  • 1,698
  • 1
  • 20
  • 29
  • 3
    You are right, of course. But my question is not about usage of types. It's specifically about creation unidirectional channels via `make`. – Ivan Velichko Apr 22 '16 at 16:40
  • 1
    The directionality of the channels is part of the type - just like the element type. It is explicitly mentioned in the [spec](https://golang.org/ref/spec#Channel_types) – matt.s Apr 22 '16 at 17:01