https://github.com/dolmen-go/contextio
Context-aware I/O streams (Writer, Reader) for Go
https://github.com/dolmen-go/contextio
context go golang-module hacktoberfest
Last synced: 17 days ago
JSON representation
Context-aware I/O streams (Writer, Reader) for Go
- Host: GitHub
- URL: https://github.com/dolmen-go/contextio
- Owner: dolmen-go
- License: apache-2.0
- Created: 2018-11-06T16:16:07.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-21T14:53:21.000Z (about 2 years ago)
- Last Synced: 2025-03-23T23:42:53.557Z (about 1 month ago)
- Topics: context, go, golang-module, hacktoberfest
- Language: Go
- Homepage: https://pkg.go.dev/github.com/dolmen-go/contextio
- Size: 14.6 KB
- Stars: 53
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# contextio - Context-aware I/O streams for Go
[](https://godoc.org/github.com/dolmen-go/contextio)
[](https://github.com/emersion/stability-badges#stable)
[](https://codecov.io/gh/dolmen-go/contextio)
[](https://travis-ci.org/dolmen-go/contextio)
[](https://goreportcard.com/report/github.com/dolmen-go/contextio)Author: [@dolmen](https://github.com/dolmen) (Olivier Mengué).
# Example
`go test -run ExampleWriter`
```go
func main() {
// interrupt context after 500ms
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
// interrupt context with SIGTERM (CTRL+C)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
go func() {
<-sigs
cancel()
}()f, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
if err != nil {
log.Fatal(err)
}
defer f.Close()// Writer that fails when context is canceled
out := contextio.NewWriter(ctx, f)// Infinite loop. Will only be interrupted once write fails.
for {
if _, err := out.Write([]byte{'a', 'b', 'c'}); err != nil {
fmt.Println("Err:", err)
break
}
}fmt.Println("Closing.")
}
```# See Also
* [github.com/jbenet/go-context/io](https://godoc.org/github.com/jbenet/go-context/io) Context-aware reader and writer
* [github.com/northbright/ctx/ctxcopy](https://godoc.org/github.com/northbright/ctx/ctxcopy) Context-aware io.Copy
* [gitlab.com/streamy/concon](https://godoc.org/gitlab.com/streamy/concon) Context-aware net.Conn