pre_go18.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !go1.8
  5. package context
  6. import "time"
  7. // A CancelFunc tells an operation to abandon its work.
  8. // A CancelFunc does not wait for the work to stop.
  9. // After the first call, subsequent calls to a CancelFunc do nothing.
  10. type CancelFunc func()
  11. // A Context carries a deadline, a cancelation signal, and other values across
  12. // API boundaries.
  13. //
  14. // Context's methods may be called by multiple goroutines simultaneously.
  15. type Context interface {
  16. // Deadline returns the time when work done on behalf of this context
  17. // should be canceled. Deadline returns ok==false when no deadline is
  18. // set. Successive calls to Deadline return the same results.
  19. Deadline() (deadline time.Time, ok bool)
  20. // Done returns a channel that's closed when work done on behalf of this
  21. // context should be canceled. Done may return nil if this context can
  22. // never be canceled. Successive calls to Done return the same value.
  23. //
  24. // WithCancel arranges for Done to be closed when cancel is called;
  25. // WithDeadline arranges for Done to be closed when the deadline
  26. // expires; WithTimeout arranges for Done to be closed when the timeout
  27. // elapses.
  28. //
  29. // Done is provided for use in select statements:
  30. //
  31. // // Stream generates values with DoSomething and sends them to out
  32. // // until DoSomething returns an error or ctx.Done is closed.
  33. // func Stream(ctx context.Context, out chan<- Value) error {
  34. // for {
  35. // v, err := DoSomething(ctx)
  36. // if err != nil {
  37. // return err
  38. // }
  39. // select {
  40. // case <-ctx.Done():
  41. // return ctx.Err()
  42. // case out <- v:
  43. // }
  44. // }
  45. // }
  46. //
  47. // See http://blog.golang.org/pipelines for more examples of how to use
  48. // a Done channel for cancelation.
  49. Done() <-chan struct{}
  50. // Err returns a non-nil error value after Done is closed. Err returns
  51. // Canceled if the context was canceled or DeadlineExceeded if the
  52. // context's deadline passed. No other values for Err are defined.
  53. // After Done is closed, successive calls to Err return the same value.
  54. Err() error
  55. // Value returns the value associated with this context for key, or nil
  56. // if no value is associated with key. Successive calls to Value with
  57. // the same key returns the same result.
  58. //
  59. // Use context values only for request-scoped data that transits
  60. // processes and API boundaries, not for passing optional parameters to
  61. // functions.
  62. //
  63. // A key identifies a specific value in a Context. Functions that wish
  64. // to store values in Context typically allocate a key in a global
  65. // variable then use that key as the argument to context.WithValue and
  66. // Context.Value. A key can be any type that supports equality;
  67. // packages should define keys as an unexported type to avoid
  68. // collisions.
  69. //
  70. // Packages that define a Context key should provide type-safe accessors
  71. // for the values stores using that key:
  72. //
  73. // // Package user defines a User type that's stored in Contexts.
  74. // package user
  75. //
  76. // import "golang.org/x/net/context"
  77. //
  78. // // User is the type of value stored in the Contexts.
  79. // type User struct {...}
  80. //
  81. // // key is an unexported type for keys defined in this package.
  82. // // This prevents collisions with keys defined in other packages.
  83. // type key int
  84. //
  85. // // userKey is the key for user.User values in Contexts. It is
  86. // // unexported; clients use user.NewContext and user.FromContext
  87. // // instead of using this key directly.
  88. // var userKey key = 0
  89. //
  90. // // NewContext returns a new Context that carries value u.
  91. // func NewContext(ctx context.Context, u *User) context.Context {
  92. // return context.WithValue(ctx, userKey, u)
  93. // }
  94. //
  95. // // FromContext returns the User value stored in ctx, if any.
  96. // func FromContext(ctx context.Context) (*User, bool) {
  97. // u, ok := ctx.Value(userKey).(*User)
  98. // return u, ok
  99. // }
  100. Value(key interface{}) interface{}
  101. }