From 9db7ef561462606085759a2f8a93b7224fdfd2fc Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 8 Mar 2016 00:07:18 +0000 Subject: context: add the context package from golang.org/x/net/context This copies the golang.org/x/net/context package to the standard library. It is imported from the x/net repo's git rev 1d9fd3b8333e (the most recent modified to x/net/context as of 2016-03-07). The corresponding change to x/net/context is in https://golang.org/cl/20347 Updates #14660 Change-Id: Ida14b1b7e115194d6218d9ac614548b9f41641cc Reviewed-on: https://go-review.googlesource.com/20346 Reviewed-by: Sameer Ajmani Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/context/withtimeout_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/context/withtimeout_test.go (limited to 'src/context/withtimeout_test.go') diff --git a/src/context/withtimeout_test.go b/src/context/withtimeout_test.go new file mode 100644 index 0000000000..3ab6fc347f --- /dev/null +++ b/src/context/withtimeout_test.go @@ -0,0 +1,25 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package context_test + +import ( + "context" + "fmt" + "time" +) + +func ExampleWithTimeout() { + // Pass a context with a timeout to tell a blocking function that it + // should abandon its work after the timeout elapses. + ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond) + select { + case <-time.After(200 * time.Millisecond): + fmt.Println("overslept") + case <-ctx.Done(): + fmt.Println(ctx.Err()) // prints "context deadline exceeded" + } + // Output: + // context deadline exceeded +} -- cgit v1.3-5-g9baa