Go Context.WithCancel 🎯

beginner
6 min

Go Context.WithCancel 🎯

Welcome to our deep dive into the context.WithCancel functionality in Go! This powerful tool is a game-changer for managing concurrent tasks, particularly in scenarios where you need to cancel long-running processes or goroutines. Let's learn together!

Introduction 📝

Before we dive into context.WithCancel, let's brush up on a few basics:

  1. Concurrency: Go's unique approach to managing concurrent tasks using goroutines and channels.
  2. Channels: A built-in Go feature used for communication between goroutines.

Now, let's dive into the main topic.

Understanding Context.WithCancel 💡

In Go, the context package helps pass data between multiple layers of a program while providing a means to cancel the operation. The context.WithCancel function is used to create a new context with a cancellation signal.

go
package main import ( "context" "fmt" "time" ) func main() { ctx, cancel := context.WithCancel(context.Background()) // Create a new context with cancellation signal go longRunningTask(ctx) // Start a long-running task time.Sleep(5 * time.Second) // Simulate some time passing cancel() // Cancel the context time.Sleep(5 * time.Second) // Check if long-running task was cancelled } func longRunningTask(ctx context.Context) { for { select { case <-ctx.Done(): // Check if context is done (cancelled) fmt.Println("Long-running task was cancelled.") return default: // Your long-running task logic here time.Sleep(1 * time.Second) } } }

In the above example, we create a new context (ctx) and a cancellation function (cancel) using context.WithCancel(context.Background()). We then start a long-running task (longRunningTask) using this context. After a short sleep, we cancel the context, which causes the long-running task to stop.

Real-world Usage 💡

In real-world scenarios, context.WithCancel is used to manage long-running operations like network requests, database transactions, or heavy computations. By cancelling the context, you can gracefully terminate these operations when necessary.

Quiz 🎯

Quick Quiz
Question 1 of 1

What does the `context.WithCancel` function do in Go?

Advanced Usage 💡

To cancel multiple contexts simultaneously, you can use the context.WithValue function to associate a value with the context and then compare these values when checking for cancellation.

go
package main import ( "context" "fmt" "sync" "time" ) func main() { parentCtx, cancelParent := context.WithCancel(context.Background()) childCtx, cancelChild := context.WithCancel(parentCtx) go longRunningTask(childCtx) go anotherLongRunningTask(parentCtx) time.Sleep(5 * time.Second) cancelParent() time.Sleep(5 * time.Second) } func longRunningTask(ctx context.Context) { ticker := time.NewTicker(1 * time.Second) defer ticker.Stop() for { select { case <-ctx.Done(): fmt.Println("Long-running task was cancelled.") return case <-ticker.C: // Your long-running task logic here } } } func anotherLongRunningTask(ctx context.Context) { ticker := time.NewTicker(2 * time.Second) defer ticker.Stop() for { select { case <-ctx.Done(): fmt.Println("Another long-running task was cancelled.") return case <-ticker.C: // Your another long-running task logic here } } }

In this example, we have two long-running tasks (longRunningTask and anotherLongRunningTask) running concurrently. When we cancel the parent context (parentCtx), both child contexts (childCtx and the one associated with anotherLongRunningTask) are cancelled.