-
-
Notifications
You must be signed in to change notification settings - Fork 870
Add solution for Challenge 30 by MuraliMohan-2000 #1151
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
base: main
Are you sure you want to change the base?
Add solution for Challenge 30 by MuraliMohan-2000 #1151
Conversation
WalkthroughIntroduces a new context management abstraction in a solution template file. Defines a Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@challenge-30/submissions/MuraliMohan-2000/solution-template.go`:
- Around line 23-25: The ExecuteWithContext implementation currently spawns a
goroutine for task func() error which cannot observe cancellation; change the
task signature to task func(context.Context) error in the interface and all call
sites, modify the ExecuteWithContext method to call task(ctx) (not task()) so
the task can inspect ctx.Done() and return promptly on cancellation, and update
any related tests/implementations that implement ExecuteWithContext to accept
and forward the context.
- Around line 38-45: Several methods (CreateCancellableContext,
CreateTimeoutContext, AddValue, GetValue, ExecuteWithContext, WaitForCompletion,
SimulateWork, and ProcessItems) contain unreachable panic("implement me")
statements immediately after return statements which causes Go compile errors;
remove those trailing panic calls so the function returns are the final
statements (e.g., delete the panic("implement me") line in
CreateCancellableContext that follows the return from
context.WithCancel(parent)) and ensure no other methods in the file contain
return-followed-by-panic patterns.
🧹 Nitpick comments (1)
challenge-30/submissions/MuraliMohan-2000/solution-template.go (1)
55-60: Use typed context keys to avoid collisions.Using plain string keys risks collisions across packages and violates the usual
context.WithValueguidance. Define a private key type and constants, then use those keys inAddValueandmain.🔐 Example adjustment
// near top-level type ctxKey string const ( ctxKeyUser ctxKey = "user" ctxKeyRequestID ctxKey = "requestID" )-ctx = cm.AddValue(ctx, "user", "alice") -ctx = cm.AddValue(ctx, "requestID", "12345") +ctx = cm.AddValue(ctx, ctxKeyUser, "alice") +ctx = cm.AddValue(ctx, ctxKeyRequestID, "12345")Also applies to: 161-163
| // Execute a task with context cancellation support | ||
| ExecuteWithContext(ctx context.Context, task func() error) error | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
cd challenge-30/submissions/MuraliMohan-2000 && cat -n solution-template.go | head -100Repository: RezaSi/go-interview-practice
Length of output: 4208
Context cancellation doesn't reach the task function.
ExecuteWithContext returns on ctx.Done(), but the task keeps running in its goroutine because it never receives the context. The task function signature func() error doesn't accept a context parameter, so it has no way to check for cancellation or respond to it. This defeats the purpose of context cancellation and can cause side effects to continue after the function returns.
Fix: Change the task signature to accept context: task func(context.Context) error, and pass it when calling the task.
🤖 Prompt for AI Agents
In `@challenge-30/submissions/MuraliMohan-2000/solution-template.go` around lines
23 - 25, The ExecuteWithContext implementation currently spawns a goroutine for
task func() error which cannot observe cancellation; change the task signature
to task func(context.Context) error in the interface and all call sites, modify
the ExecuteWithContext method to call task(ctx) (not task()) so the task can
inspect ctx.Done() and return promptly on cancellation, and update any related
tests/implementations that implement ExecuteWithContext to accept and forward
the context.
| // CreateCancellableContext creates a cancellable context | ||
| func (cm *simpleContextManager) CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc) { | ||
| // TODO: Implement cancellable context creation | ||
| // Hint: Use context.WithCancel(parent) | ||
|
|
||
| return context.WithCancel(parent) | ||
| panic("implement me") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Check if the file exists and examine lines 38-45
if [ -f "challenge-30/submissions/MuraliMohan-2000/solution-template.go" ]; then
echo "=== File content (lines 30-50) ==="
sed -n '30,50p' "challenge-30/submissions/MuraliMohan-2000/solution-template.go" | cat -n
else
echo "File not found at expected path"
# Try to find it
find . -name "solution-template.go" -type f 2>/dev/null | head -5
fiRepository: RezaSi/go-interview-practice
Length of output: 1080
🏁 Script executed:
#!/bin/bash
# Search for all panic statements in the file
echo "=== All panic statements in the file ==="
rg "panic\(" "challenge-30/submissions/MuraliMohan-2000/solution-template.go" -n -B 2 -A 0Repository: RezaSi/go-interview-practice
Length of output: 718
🏁 Script executed:
#!/bin/bash
# Check the full context of functions that may have return+panic patterns
echo "=== All function definitions and their return statements ==="
rg "^func|return|panic" "challenge-30/submissions/MuraliMohan-2000/solution-template.go" -nRepository: RezaSi/go-interview-practice
Length of output: 1661
Remove unreachable panic statements—Go compiler rejects this code.
Unreachable code after return causes compilation failure. This pattern appears in 8 methods throughout the file: CreateCancellableContext, CreateTimeoutContext, AddValue, GetValue, ExecuteWithContext, WaitForCompletion, SimulateWork, and ProcessItems. Delete all panic("implement me") lines that follow return statements.
Example fix for CreateCancellableContext (apply to all affected methods)
func (cm *simpleContextManager) CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc) {
// TODO: Implement cancellable context creation
// Hint: Use context.WithCancel(parent)
return context.WithCancel(parent)
- panic("implement me")
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // CreateCancellableContext creates a cancellable context | |
| func (cm *simpleContextManager) CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc) { | |
| // TODO: Implement cancellable context creation | |
| // Hint: Use context.WithCancel(parent) | |
| return context.WithCancel(parent) | |
| panic("implement me") | |
| } | |
| // CreateCancellableContext creates a cancellable context | |
| func (cm *simpleContextManager) CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc) { | |
| // TODO: Implement cancellable context creation | |
| // Hint: Use context.WithCancel(parent) | |
| return context.WithCancel(parent) | |
| } |
🤖 Prompt for AI Agents
In `@challenge-30/submissions/MuraliMohan-2000/solution-template.go` around lines
38 - 45, Several methods (CreateCancellableContext, CreateTimeoutContext,
AddValue, GetValue, ExecuteWithContext, WaitForCompletion, SimulateWork, and
ProcessItems) contain unreachable panic("implement me") statements immediately
after return statements which causes Go compile errors; remove those trailing
panic calls so the function returns are the final statements (e.g., delete the
panic("implement me") line in CreateCancellableContext that follows the return
from context.WithCancel(parent)) and ensure no other methods in the file contain
return-followed-by-panic patterns.
Challenge 30 Solution
Submitted by: @MuraliMohan-2000
Challenge: Challenge 30
Description
This PR contains my solution for Challenge 30.
Changes
challenge-30/submissions/MuraliMohan-2000/solution-template.goTesting
Thank you for reviewing my submission! 🚀