Context should go away for Go 2 (2017)
faiface.github.io
Context should go away for Go 2 (2017)
1–10 of 176 posts
Re: Context should go away for Go 2 (2017)
#2Re: Context should go away for Go 2 (2017)
#3Needs a (2017)!
Re: Context should go away for Go 2 (2017)
#4 n, err := r.Read(context.TODO(), p)
> put a bullet in my head, please.Manually passing around a context everywhere sounds about as palatable as manually checking every return for error.
Re: Context should go away for Go 2 (2017)
#5It's funny, it really was just using strings as keys until quite recently, and obviously there were collisions and there was no way to "protect" a key/value, etc.
Now the convention is to use a key with a private type, so no more collisions. The value you get is still untyped and needs to be cast, though. Also there are still many older libraries still uses strings.
Re: Context should go away for Go 2 (2017)
#6okay... so they dodged the thing I thought was going to be interesting, how would you solve passing state? e.g. if I write a middleware for net/http, I have to duplicate the entire http.Request, and add my value to it.
Re: Context should go away for Go 2 (2017)
#7> If the Go language ever comes to the point where I’d have to write this n, err := r.Read(context.TODO(), p) > put a bullet in my head, please. Manually passing around a context everywhere sounds about as palatable as manually checking every return for error.
Re: Context should go away for Go 2 (2017)
#8I kind of do wish we had goroutine local storage though :) Passing down the context of the request everywhere is ugly.
Re: Context should go away for Go 2 (2017)
#9As others have already mentioned, there won't be a Go 2. Besides, I really don't want another verbose method for cancellation; error handling is already bad enough.
Re: Context should go away for Go 2 (2017)
#10 func CancellableOp(done chan error /* , args... */) {
for {
// ...
// cancellable code:
select {
case
Some compare context "virus" to async virus in languages that bolt-on async runtime on top of sync syntax - but the main difference is you can compose context-aware code with context-oblivious code (by passing context.Background()), and vice versa with no problems. E.g. here's a context-aware wrapper for the standard `io.Reader` that is completely compatible with `io.Reader`: type ioContextReader struct {
io.Reader
ctx context.Context
}
func (rc ioContextReader) Read(p []byte) (n int, err error) {
done := make(chan struct{})
go func() {
n, err = rc.Reader.Read(p)
close(done)
}()
select {
case
For io.ReadCloser, we could call `Close()` method when context exits, or even better, with `context.AfterFunc(ctx, rc.Close)`.Contexts definitely have flaws - verbosity being the one I hate the most - but having them behave as ordinary values, just like errors, makes context-aware code more understandable and flexible.
And just like errors, having cancellation done automatically makes code more prone to errors. When you don't put "on-cancel" code, your code gets cancelled but doesn't clean up after itself. When you don't select on `ctx.Done()` your code doesn't get cancelled at all, making the bug more obvious.