Skip to content

Instantly share code, notes, and snippets.

@guerbai
Last active June 12, 2019 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guerbai/6bd7d07c0edcf959a8fd9bba6697e33c to your computer and use it in GitHub Desktop.
Save guerbai/6bd7d07c0edcf959a8fd9bba6697e33c to your computer and use it in GitHub Desktop.
calculate函数,闭包与传函数
type operate func(x, y int) int
func calculate(x int, y int, op operate) (int, error) {
if op == nil {
return 0, error.New("no op")
}
return op(x, y), nil
}
var add = func (x, y int) int {
return x + y
}
v, _ := operate(1, 2, add)
fmt.Printf(v)
func genCalculator(op operate) operate {
return func(x, y int) int {
return op(x, y)
}
}
add := genCalculator(func(x, y) int {
return x + y
})
v := add(5, 6)
fmt.Println(v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment