Skip to content

Latest commit

 

History

History
25 lines (17 loc) · 1.3 KB

File metadata and controls

25 lines (17 loc) · 1.3 KB

Hints for Bank Account with Error Handling

Hint 1: Struct Design

Design an Account struct with fields for ID, owner name, and balance. Consider what data types are appropriate for each field.

Hint 2: Error Types

Think about what can go wrong in banking operations: insufficient funds, invalid amounts, account not found, etc. Create custom error types for these scenarios.

Hint 3: Custom Error Implementation

Implement the error interface by creating a struct with an Error() string method. Include relevant context in your error messages.

Hint 4: Input Validation

Always validate inputs before performing operations. Check for negative amounts, nil pointers, and other invalid conditions.

Hint 5: Balance Checking

For withdrawal operations, check if the account has sufficient funds before modifying the balance. Return an appropriate error if not.

Hint 6: Thread Safety

Use sync.Mutex to protect account operations from race conditions when multiple goroutines access the same account.

Hint 7: Error Wrapping

Use fmt.Errorf with the %w verb to wrap errors and provide additional context while preserving the original error.

Hint 8: Testing Error Scenarios

Write tests that verify both successful operations and error conditions. Use type assertions to check for specific error types.