Add solution for Challenge 3 by Miniuzzo#1460
Conversation
WalkthroughThis change introduces a Manager system in Go within a solution template. It defines Employee and Manager structs with methods to add employees, remove by ID, calculate average salary, and find employees by ID, along with a main function demonstrating these operations. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). 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 |
| func (m *Manager) FindEmployeeByID(id int) *Employee { | ||
|
|
||
| for _, emp := range m.Employees { | ||
| if emp.ID == id { | ||
| return &emp | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } |
There was a problem hiding this comment.
Bug: Returns pointer to loop variable copy, not the actual slice element.
emp is a copy of each Employee in the slice. Returning &emp gives a pointer to that copy, not to the element stored in m.Employees. Any modifications through the returned pointer won't affect the original data in the Manager's slice.
Use index-based iteration to return a pointer to the actual slice element.
🐛 Proposed fix
func (m *Manager) FindEmployeeByID(id int) *Employee {
-
- for _, emp := range m.Employees {
- if emp.ID == id {
- return &emp
+ for i := range m.Employees {
+ if m.Employees[i].ID == id {
+ return &m.Employees[i]
}
}
-
return nil
}📝 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.
| func (m *Manager) FindEmployeeByID(id int) *Employee { | |
| for _, emp := range m.Employees { | |
| if emp.ID == id { | |
| return &emp | |
| } | |
| } | |
| return nil | |
| } | |
| func (m *Manager) FindEmployeeByID(id int) *Employee { | |
| for i := range m.Employees { | |
| if m.Employees[i].ID == id { | |
| return &m.Employees[i] | |
| } | |
| } | |
| return nil | |
| } |
Challenge 3 Solution
Submitted by: @Miniuzzo
Challenge: Challenge 3
Description
This PR contains my solution for Challenge 3.
Changes
challenge-3/submissions/Miniuzzo/solution-template.goTesting
Thank you for reviewing my submission! 🚀