84
GitHub - machinebox/progress: io.Reader and io.Writer with progress and remainin...
source link: https://github.com/machinebox/progress
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
progress
io.Reader
and io.Writer
with progress and remaining time estimation.
Usage
ctx := context.Background()
// get a reader and the total expected number of bytes
s := `Now that's what I call progress`
size := len(s)
r := progress.NewReader(strings.NewReader(s))
// Start a goroutine printing progress
go func() {
ctx := context.Background()
progressChan := progress.NewTicker(ctx, r, size, 1*time.Second)
for p := range progressChan {
fmt.Printf("\r%v remaining...", p.Remaining().Round(time.Second))
}
fmt.Println("\rdownload is completed")
}()
// use the Reader as normal
if _, err := io.Copy(dest, r); err != nil {
log.Fatalln(err)
}
- Wrap an
io.Reader
orio.Writer
withNewReader
andNewWriter
respectively - Capture the total number of expected bytes
- Use
progress.NewTicker
to get a channel on which progress updates will be sent - Start a Goroutine to periodically check the progress, and do something with it - like log it
- Use the readers and writers as normal
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK