8

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)
6
  • 2
    There is no point really; don't make directional channels.
    – JimB
    Commented Apr 22, 2016 at 16:11
  • @JimB, could you explain why, please? Commented Apr 22, 2016 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
    Commented Apr 22, 2016 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? Commented Apr 22, 2016 at 16:38
  • 4
    yes, directional channels are used all the time, you just don't make them that way.
    – JimB
    Commented Apr 22, 2016 at 16:44

2 Answers 2

5

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.

2
  • Hm, I can create a regular channel and pass it to the function which expects write-only channel. Commented Apr 22, 2016 at 16:44
  • 1
    Right, how about 3-d party libraries that take non directional channel but we want to test it does not read/write? Commented Apr 22, 2016 at 17:01
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.

2
  • 3
    You are right, of course. But my question is not about usage of types. It's specifically about creation unidirectional channels via make. Commented Apr 22, 2016 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
    – matt.s
    Commented Apr 22, 2016 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.