📝 Add WithContext example to README.md
This commit is contained in:
parent
09a24ce720
commit
76c3e62228
1 changed files with 36 additions and 0 deletions
36
README.md
36
README.md
|
|
@ -87,6 +87,42 @@ func main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Using WithContext
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"codeberg.org/danjones000/waiterr"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
we, ctx := waiterr.WithContext(ctx)
|
||||||
|
|
||||||
|
we.Go(func() error {
|
||||||
|
select {
|
||||||
|
case <-time.After(100 * time.Millisecond):
|
||||||
|
fmt.Println("Task completed")
|
||||||
|
return nil
|
||||||
|
case <-ctx.Done():
|
||||||
|
fmt.Println("Task cancelled")
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
_ = we.Wait()
|
||||||
|
// Output:
|
||||||
|
// Task completed
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
Please refer to the [AGENTS.md](AGENTS.md) file for guidelines on contributing to this project, including code style, commit messages, and Git workflow.
|
Please refer to the [AGENTS.md](AGENTS.md) file for guidelines on contributing to this project, including code style, commit messages, and Git workflow.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue