Created
October 2, 2021 20:28
-
-
Save mkock/8a8aed5c03ef46a3b5156b8a84774aef to your computer and use it in GitHub Desktop.
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
| type calculator interface { | |
| add(a, b int) int | |
| sub(a, b int) int | |
| div(a, b int) int | |
| mul(a, b int) int | |
| } | |
| type mockCalculator struct { | |
| calculator // Embeds the calculator interface | |
| } | |
| func (m *mockCalculator) add(a, b int) int { return a + b } | |
| func summarize(calc calculator, values ...int) int { | |
| var result int | |
| for _, val := range values { | |
| result = calc.add(result, val) | |
| } | |
| return result | |
| } | |
| func main() { | |
| mock := new(mockCalculator) | |
| result := summarize(mock, 10, 12, 9, 11) | |
| fmt.Println(result) // Prints 42 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment