Skip to content
Closed
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions internal/exercises/catalog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,14 @@ projects:
- "Use time.Parse() to parse time strings with known layouts"
- "Use time.LoadLocation() to work with different timezones"
- "Extract time components using .Date() and .Clock() methods"

- slug: 39_waitgroup
title: "WaitGroups"
difficulty: beginner
topics: ["concurrency", "sync", "goroutines", "waitgroup"]
hints:
- "Use sync.WaitGroup to wait for goroutines."
- "Call wg.Add(n) before starting n goroutines; each must call wg.Done()."
- "Call wg.Wait() in the parent to block until completion."


21 changes: 21 additions & 0 deletions internal/exercises/solutions/39_waitgroup/waitGroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package waitgroup

import (
"sync"
)

// The waitGroup in Go is used to wait for a collection of goroutines to finish executing

func worker(wg *sync.WaitGroup, result *string) {
defer wg.Done()
*result = "Worker done"
}

func waitGroup() string {
var wg sync.WaitGroup
result := ""
wg.Add(1)
go worker(&wg, &result)
wg.Wait()
return result
}
18 changes: 18 additions & 0 deletions internal/exercises/templates/39_waitgroup/waitgroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package waitgroup

import (
"sync"
)

// TODO: Implement these functions so tests pass
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give one liner hints if possible.


func worker(wg *sync.WaitGroup, result *string) {
*result = "Worker done"
}

func waitGroup() string {
var wg sync.WaitGroup
result := ""
go worker(&wg, &result)
return result
}
11 changes: 11 additions & 0 deletions internal/exercises/templates/39_waitgroup/waitgroup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package waitgroup

import "testing"

func TestWaitGroup(t *testing.T) {
got := waitGroup()
want := "Worker done"
if got != want {
t.Fatalf("waitGroup() = %q, want %q", got, want)
}
}