From b9155d15d3bd9fb4851196803c5c7447b620d3b4 Mon Sep 17 00:00:00 2001 From: nxshock Date: Wed, 3 Feb 2021 00:31:01 +0500 Subject: [PATCH] Add code example --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index c738b9e..7f68026 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,41 @@ Simple goroutine worker pool written in Go. Status: Work in progress. + +Example: + +```go +package main + +import ( + "log" + "time" + + "github.com/nxshock/gwp" +) + +// Job function +func f(i int) error { + log.Printf("Job №%d", i) + + // Simulate work + time.Sleep(time.Second) + + return nil +} + +func main() { + worker := gwp.New(4) + + for i := 0; i < 100; i++ { + n := i + // Send job + worker.Add(func() error { + return f(n) + }) + } + + // Wait all jobs to complete + worker.CloseAndWait() +} +```