Skip to content

πŸ“ A drop-in replacement to io.Reader and io.Writer with the total number of bytes transfered.

License

Notifications You must be signed in to change notification settings

samber/go-metered-io

Repository files navigation

Metered io.Reader and io.Writer

tag Go Version GoDoc Build Status Go report Coverage Contributors License

A drop-in replacement to io.Reader and io.Writer from the standard library with the total number of bytes transfered.

πŸš€ Install

go get github.com/samber/go-metered-io

This library is v1 and follows SemVer strictly. No breaking changes will be made to exported APIs before v2.0.0.

πŸ’‘ Spec

GoDoc: https://pkg.go.dev/github.com/samber/go-metered-io

Byte:

  • metered.NewReader
  • metered.NewWriter
  • metered.NewReadCloser
  • metered.NewWriteCloser
  • metered.NewReadWriter

String:

  • metered.NewStringWriter

Rune:

  • metered.NewRuneReader

Examples

Metered reader

import "github.com/samber/go-metered-io"

r := metered.NewReader(strings.NewReader("Lorem ipsum dolor sit amet..."))

for {
    buff := make([]byte, 10)

    _, err := r.Read(buff)
    if err != nil {
        break
    }
}

fmt.Printf("Total bytes: %d", r.Rx())

Metered writer

import "github.com/samber/go-metered-io"

var buf bytes.Buffer
w := metered.NewWriter(&buf)

for i := 0 ; i < 10 ; i++ {
    _, err := w.Write("Hello world\n")
    if err != nil {
        break
    }
}

fmt.Printf("Total bytes: %d", w.Tx())

🀝 Contributing

Don't hesitate ;)

# Install some dev dependencies
make tools

# Run tests
make test
# or
make watch-test

πŸ‘€ Contributors

Contributors

πŸ’« Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

πŸ“ License

Copyright Β© 2023 Samuel Berthe.

This project is MIT licensed.