Skip to content

Commit

Permalink
Merge pull request rstacruz#1339 from r3s/patch-2
Browse files Browse the repository at this point in the history
Update go.md
  • Loading branch information
rstacruz authored Feb 13, 2020
2 parents ebe8a85 + 0e9b733 commit 2b20b85
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions go.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,39 @@ v, ok := <- ch

See: [Range and close](https://tour.golang.org/concurrency/4)

### WaitGroup

```go
import "sync"

func main() {
var wg sync.WaitGroup

for _, item := range itemList {
// Increment WaitGroup Counter
wg.Add(1)
go doOperation(item)
}
// Wait for goroutines to finish
wg.Wait()

}
```
{: data-line="1,4,8,12"}

```go
func doOperation(item string) {
defer wg.Done()
// do operation on item
// ...
}
```
{: data-line="2"}

A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. The goroutine calls `wg.Done()` when it finishes.
See: [WaitGroup](https://golang.org/pkg/sync/#WaitGroup)


## Error control

### Defer
Expand Down

0 comments on commit 2b20b85

Please sign in to comment.