-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: spec: provide way to easily turn channel into readable-only / writable-only channel #22189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I don’t think there is anything to do here, solution 1 *is* the solution here. If you disagree can you please provide a source code example showing the limitations of solution 1.
… On 10 Oct 2017, at 08:58, leonklingele ***@***.***> wrote:
This has been sitting in my queue for quite a while now and after finding #21953, I think it's time to publish this proposal.
Proposal
The problem
Consider the following example:
You create a channel and pass it directly to a producer and a consumer; the producer puts data onto the channel, while the consumer reads this data from the channel.
Although this works fine, there's a drawback: a consumer might as well read data from the channel instead of writing to it and a producer may write to the channel instead of reading from it.
Existing solutions
There are three already existing solutions to this problem:
Specify in the function signature of the producer and consumer what they should be allowed to do with a channel (func producer(c chan<- *data) / func consumer(c <-chan *data))
Create concrete helper functions to turn an RW channel to an R-only / W-only channel as illustrated in this example: https://play.golang.org/p/ats2aDDbEi
Define an interface (applicable to some cases only)
The problem with these solutions
Solution 1. is too permissive, permissions to read / write should be granted by the caller (the main func in the given example), not the callee (i.e. producer / consumer)
Solution 2. works, but is ugly. It shouldn't be that hard to make a channel readable / writeable only. Also, as long as Go has no generics (proposal here), these two helper functions (onlyReadable / onlyWritable) are required for every type which is sent over a channel.
A better solution?
Only aware of these solutions, I propose a fourth:
// producer may only write into the channel c, or close it
go producer(c<-)
// consumer may only read from the channel c
consumer(<-c)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
You can do solution 1, and enforce it in main, like this:
|
https://play.golang.org/p/m8ZabPnjv6 Also, quoting myself
@randall77 interesting, I wasn't aware of this. Thanks. var cr <-chan int = c
consumer(cr) which not only requires a new dedicated variable, but also is harder to read / grasp. |
Please seek Keiths example, this is how to do it in Go if you don’t trust the author of the function. |
This is a subjective argument. In general Go does not aim for maximum brevity, it aims for maximum clarity even if that involves an extra line or two. |
The more I look at my example, the more I dislike the proposed solution. |
This has been sitting in my queue for quite a while now and after finding #21953, I think it's time to publish this proposal.
Proposal
The problem
Consider the following example:
You create a channel and pass it directly to a producer and a consumer; the producer puts data onto the channel, while the consumer reads this data from the channel.
Although this works fine, there's a drawback: a consumer might as well read data from the channel instead of writing to it and a producer may write to the channel instead of reading from it.
Existing solutions
There are three already existing solutions to this problem:
Specify in the function signature of the producer and consumer what they should be allowed to do with a channel (
func producer(c chan<- *data)
/func consumer(c <-chan *data)
)Create concrete helper functions to turn an RW channel to an R-only / W-only channel as illustrated in this example: https://play.golang.org/p/ats2aDDbEi
Define an interface (applicable to some cases only)
The problem with these solutions
Solution 1. is too permissive, permissions to read / write should be granted by the caller (the
main
func in the given example), not the callee (i.e.producer
/consumer
)Solution 2. works, but is ugly. It shouldn't be that hard to make a channel readable / writeable only. Also, as long as Go has no generics (proposal here), these two helper functions (
onlyReadable
/onlyWritable
) are required for every type which is sent over a channel.A better solution?
Only aware of these solutions, I propose a fourth:
The text was updated successfully, but these errors were encountered: