Skip to content

Instantly share code, notes, and snippets.

@converge
Created February 7, 2024 17:01
Show Gist options
  • Save converge/833662ca5a76599d750dc50b4d4e1a8f to your computer and use it in GitHub Desktop.
Save converge/833662ca5a76599d750dc50b4d4e1a8f to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"sync"
"time"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel() // Ensure cancellation even if main exits early
studioCall(ctx, doStuff)
}
func doStuff(ctx context.Context, wg *sync.WaitGroup) {
for {
select {
case <-ctx.Done():
fmt.Println("exiting...")
wg.Done()
return // Exit the function when context is cancelled
default:
fmt.Println("...")
time.Sleep(time.Second) // Simulate work
}
}
}
func studioCall(ctx context.Context, userFunc func(ctx context.Context, wg *sync.WaitGroup)) {
// Create a wait group to ensure all goroutines finish before exiting
var wg sync.WaitGroup
wg.Add(1)
// Wait for all goroutines to finish before returning
defer wg.Wait()
// Call the user function with the context
userFunc(ctx, &wg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment