pre_go19.go 4.0 KB

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