From ffb165bcc4d48ac42c23fe0a73040e10c20c4cf7 Mon Sep 17 00:00:00 2001 From: nxshock Date: Wed, 3 Aug 2022 22:00:52 +0500 Subject: [PATCH] Add example --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.md b/README.md index 54cfadc..3eee66f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,44 @@ # go-eta ETA calculator for Go. + +## Example + +```go +package main + +import ( + "fmt" + "math/rand" + "os" + "time" + + "github.com/nxshock/go-eta" +) + +func main() { + stepsCount := 1000 + + eta := eta.New(time.Minute, stepsCount) + + processed := 0 + + // Emulate work + go func() { + for processed < stepsCount { + time.Sleep(time.Second) + r := rand.Intn(30) + + processed += r + eta.Increment(r) + } + }() + + // Print progress + for processed < stepsCount { + time.Sleep(time.Second) // Update progress every second + fmt.Fprintf(os.Stderr, "\rProcessed %d of %d, ETA: %s", processed, stepsCount, eta.Eta().Format("15:04:05")) + } + +} +``` \ No newline at end of file