Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more examples to README #6

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ go get github.com/kurt212/syncgroup

### Example

Run goroutines in parallel and wait until all of them finish.

```go
package main

Expand Down Expand Up @@ -56,6 +58,80 @@ func main() {
}
```

Collect errors from goroutines.

```go
package main

import (
"fmt"
"time"

"github.com/kurt212/syncgroup"
)

func main() {
sg := syncgroup.New()

for i := range 10 {
sg.Go(func() error {
time.Sleep(1 * time.Second)

return fmt.Errorf("error %d", i)
})
}

err := sg.Wait()
if err != nil {
/*
Expected output:
error 1
error 3
error 2
error 4
error 6
error 5
error 0
error 9
error 7
error 8
*/
fmt.Println(err)
}
}
```

Limit number of concurrent goroutines.

```go
package main

import (
"fmt"
"time"

"github.com/kurt212/syncgroup"
)

func main() {
sg := syncgroup.New()

sg.SetLimit(2)

for i := range 10 {
sg.Go(func() error {
fmt.Printf("Go %d\n", i)

time.Sleep(1 * time.Second)

return nil
})
}

sg.Wait()
}
```

## Contributing

Feel free to contribute to this project. You can report bugs, suggest features or submit pull requests.
Expand Down
Loading