@@ -11,16 +11,123 @@ maintaining 100% compatibility.
1111
1212### Requirements
1313
14- The minimum Go version is ` go1.23 ` .
14+ The minimum Go version is ` go1.26 ` .
1515
1616### Install
1717
18- The ` forms ` package can be added to a project with ` go get ` .
18+ The ` scope ` package can be added to a project with ` go get ` .
1919
2020``` shell
2121go get -u cattlecloud.net/go/scope@latest
2222```
2323
24+ ### Examples
25+
26+ ##### New
27+
28+ ``` go
29+ ctx := scope.New ()
30+ ```
31+
32+ ##### TTL
33+
34+ ``` go
35+ ctx , cancel := scope.TTL (5 * time.Second )
36+ // ctx is canceled after 5 seconds
37+ defer cancel ()
38+ ```
39+
40+ ##### Deadline
41+
42+ ``` go
43+ ctx , cancel := scope.Deadline (time.Now ().Add (10 * time.Second ))
44+ // ctx is canceled at the specified time
45+ defer cancel ()
46+ ```
47+
48+ ##### Cancelable
49+
50+ ``` go
51+ ctx , cancel := scope.Cancelable ()
52+ // ctx can be canceled manually
53+ defer cancel ()
54+ ```
55+
56+ ##### WithCancel
57+
58+ ``` go
59+ ctx , cancel := scope.WithCancel (parentCtx)
60+ defer cancel ()
61+ ```
62+
63+ ##### WithTTL
64+
65+ ``` go
66+ ctx , cancel := scope.WithTTL (parentCtx, 3 * time.Second )
67+ // parentCtx with a 3 second timeout
68+ defer cancel ()
69+ ```
70+
71+ ##### WithValue
72+
73+ ``` go
74+ ctx := scope.WithValue (parentCtx, " userID" , 123 )
75+ ```
76+
77+ ##### Value
78+
79+ ``` go
80+ userID := scope.Value [int ](ctx, " userID" )
81+ ```
82+
83+ ##### Join
84+
85+ ``` go
86+ ctx1 , cancel1 := scope.WithCancel (scope.New ())
87+ ctx2 , cancel2 := scope.TTL (5 * time.Second )
88+
89+ joined , cancel := scope.Join (ctx1, ctx2)
90+ // joined is canceled when ctx1 or ctx2 is canceled
91+ defer cancel ()
92+ defer cancel1 ()
93+ defer cancel2 ()
94+ ```
95+
96+ ###### Deadline
97+
98+ ``` go
99+ ctx1 , _ := scope.Deadline (time.Now ().Add (10 * time.Second ))
100+ ctx2 , _ := scope.Deadline (time.Now ().Add (20 * time.Second ))
101+
102+ joined , _ := scope.Join (ctx1, ctx2)
103+ deadline , ok := joined.Deadline () // deadline is 10 seconds, ok is true
104+ ```
105+
106+ ###### Done
107+
108+ ``` go
109+ joined , cancel := scope.Join (ctx1, ctx2)
110+ <- joined.Done () // blocks until either ctx1 or ctx2 is done
111+ ```
112+
113+ ###### Err
114+
115+ ``` go
116+ joined , cancel := scope.Join (ctx1, ctx2)
117+ <- joined.Done ()
118+ err := joined.Err () // returns the error from the first canceled context
119+ ```
120+
121+ ###### Value
122+
123+ ``` go
124+ ctx1 := scope.WithValue (scope.New (), " key" , " value1" )
125+ ctx2 := scope.WithValue (scope.New (), " key" , " value2" )
126+
127+ joined , _ := scope.Join (ctx1, ctx2)
128+ val := joined.Value (" key" ) // returns value1 (ctx1's value is checked first)
129+ ```
130+
24131### License
25132
26133The ` cattlecloud.net/go/scope ` module is open source under the [ BSD] ( LICENSE ) license.
0 commit comments