-
Notifications
You must be signed in to change notification settings - Fork 23
recover excercise new pull req #109
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
Open
Omesh2004
wants to merge
14
commits into
zhravan:main
Choose a base branch
from
Omesh2004:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6a23a15
recover_module added
Omesh2004 36e46df
chore(format): auto-format code [skip ci]
github-actions[bot] b09ddfd
doucmentation added for recover exercise
Omesh2004 d84a6a2
Merge branch 'main' of https://github.com/Omesh2004/golearn-1
Omesh2004 787d661
minor changes in catalog file recover
Omesh2004 89ad530
minor edits in recover
Omesh2004 fd41248
copied the json files from the main branch
Omesh2004 8419473
chore(format): auto-format code [skip ci]
github-actions[bot] dddd67d
minor edits in excercise.go
Omesh2004 e01e794
Merge branch 'main' of https://github.com/Omesh2004/golearn-1
Omesh2004 559ac72
added fallback for matadata title in exercises.go
Omesh2004 3338c71
chore(format): auto-format code [skip ci]
github-actions[bot] 27c96b7
minor edits
Omesh2004 a168072
Merge branch 'main' of https://github.com/Omesh2004/golearn-1
Omesh2004 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package recover_exercise | ||
|
|
||
| // DoWork simulates a function that might panic if the input is negative. | ||
| // | ||
| // It is designed to be called by Run, which should demonstrate | ||
| // how to safely handle the panic using recover(). | ||
| func DoWork(n int) { | ||
| if n < 0 { | ||
| // A panic occurs if n is negative | ||
| panic("input cannot be negative") | ||
| } | ||
| // Normal, non-panicking code path... | ||
| } | ||
|
|
||
| // Run calls DoWork and uses defer/recover to safely handle any panic. | ||
| // It returns the recovered panic value or nil if no panic occurred. | ||
| func Run(n int) (recoveredValue interface{}) { | ||
| // The deferred function is executed just before Run returns. | ||
| defer func() { | ||
| // Call recover() to check if a panic has occurred. | ||
| if r := recover(); r != nil { | ||
| // If recover() returns a non-nil value (a panic occurred), | ||
| // assign it to the named return variable 'recoveredValue'. | ||
| recoveredValue = r | ||
| } | ||
| }() | ||
|
|
||
| DoWork(n) | ||
|
|
||
| // 'recoveredValue' is returned. It will be the panic value if | ||
| // a panic occurred and was recovered, or nil otherwise. | ||
| return recoveredValue | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package recover_exercise | ||
|
|
||
| // DoWork simulates a function that might panic if the input is negative. | ||
| func DoWork(n int) { | ||
| if n < 0 { | ||
| // A panic occurs if n is negative | ||
| panic("input cannot be negative") | ||
| } | ||
| // Normal, non-panicking code path... | ||
| } | ||
|
|
||
| // Run should call DoWork and safely recover from any panic | ||
| // that occurs during its execution, returning the recovered value. | ||
| // It should return nil if no panic occurred. | ||
| func Run(n int) (recoveredValue interface{}) { | ||
| // 1. Add your 'defer' statement here. | ||
| // 2. The deferred function should call recover() and assign the result | ||
| // to 'recoveredValue' if it is not nil. | ||
|
|
||
| // Your code here: | ||
|
|
||
| DoWork(n) | ||
|
|
||
| return recoveredValue | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package recover_exercise | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestRun_NoPanic(t *testing.T) { | ||
| t.Parallel() | ||
| result := Run(10) | ||
| if result != nil { | ||
| t.Errorf("Run(10) should not panic and should return nil. Got: %v", result) | ||
| } | ||
| } | ||
|
|
||
| func TestRun_WithPanic(t *testing.T) { | ||
| t.Parallel() | ||
| expectedPanicValue := "input cannot be negative" | ||
| result := Run(-5) | ||
|
|
||
| if result == nil { | ||
| t.Errorf("Run(-5) should panic and recover, returning the panic value. Got nil") | ||
| } | ||
|
|
||
| // Check if the recovered value is what we expected | ||
| if resultAsString, ok := result.(string); !ok || resultAsString != expectedPanicValue { | ||
| t.Errorf("Run(-5) recovered with unexpected value. Expected: %q, Got: %v (%T)", expectedPanicValue, result, result) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Restore string hints to keep catalog schema valid
Every existing catalog entry provides
hintsas plain strings. The loader expects a[]string; supplying maps (hint_type,text) will fail to unmarshal and break catalog loading. Please keep hints as simple strings (or extend the loader before changing the schema).📝 Committable suggestion
🤖 Prompt for AI Agents