Chapter mini: calculator
Implement a small calculator function.
Your job is to write a function that takes two integers and an operator, then returns both a numeric result and whether the operation was valid.
Task
- Implement this function:
func calculate(a, b int, op string) (int, bool)
- The
boolreturn should mean whether the operation succeeded. - Use these examples to define the behavior:
| Call | Return |
|---|---|
calculate(8, 5, "+") |
(13, true) |
calculate(20, 4, "/") |
(5, true) |
calculate(10, 0, "/") |
(0, false) |
calculate(7, 3, "?") |
(0, false) |
- In
main, callcalculatewith those same four inputs. - When a call succeeds, print
result: <value>. - When a call fails, print
error: invalid operation.
Expected output
result: 13 result: 5 error: invalid operation error: invalid operation