Skip to content

Instantly share code, notes, and snippets.

@mkock
Created October 2, 2021 20:28
Show Gist options
  • Select an option

  • Save mkock/8a8aed5c03ef46a3b5156b8a84774aef to your computer and use it in GitHub Desktop.

Select an option

Save mkock/8a8aed5c03ef46a3b5156b8a84774aef to your computer and use it in GitHub Desktop.
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