
本文共 3230 字,大约阅读时间需要 10 分钟。
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� richer forms of composition ��� abstraction���
������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Go������������������������������
Go��������������������������������� developers��������������������������������������������� Go ���������������������������
package mainimport ( "errors" "fmt")type operate func(x, y int) intfunc calculate(x int, y int, op operate) (int, error) { if op == nil { return 0, errors.New("invalid operation") } return op(x, y), nil}func genCalculator(op operate) func(x, y int) (int, error) { return func(x int, y int) (int, error) { if op == nil { return 0, errors.New("invalid operation") } return op(x, y), nil }}func main() { //������1 x, y := 12, 23 op := func(x int, y int) int { return 12 + y } result, err := calculate(x, y, op) fmt.Printf("The result: %d (error: %v)\n", result, err) result, err = calculate(x, y, nil) fmt.Printf("The result: %d (error: %v)\n", result, err) //������2 x, y = 56, 78 add := genCalculator(op) result, err = add(x, y) fmt.Printf("The result: %d (error: %v)\n", result, err)}
���������������������calculate
������������������������ op
��������������������������������������������������� op
��� nil
������������������������genCalculator
��������������������������������������������������������� op
������������������������������
Go������������������������
Go ������������������������������������������������������������������������������������������
package mainimport ( "fmt" "reflect")func main() { var f = func(str string) { fmt.Println("hello", str) } fmt.Printf("������������%v\n", reflect.ValueOf(f).Kind()) f("func type")}
���������������������
������������ funchello func type
���������������������������������������������������������������������������
package mainimport ( "fmt" "reflect")func exec(f func(str string)) { f("������������������")}func main() { var f = func(str string) { fmt.Println("hello", str) } fmt.Printf("������������%v\n", reflect.ValueOf(f).Kind()) exec(f)}
���������������
������������ funchello ������������������
���������������������������������������������������������������exec
������������������������������������
发表评论
最新留言
关于作者
