Go 学习笔记(61)— Go 高阶函数(函数作为输入参数、返回值、变量)的写法
发布日期:2021-05-10 08:44:35 浏览次数:19 分类:精选文章

本文共 3230 字,大约阅读时间需要 10 分钟。

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� richer forms of composition ��� abstraction���

������������������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

Go������������������������������

Go��������������������������������� developers��������������������������������������������� Go ���������������������������

package main
import (
"errors"
"fmt"
)
type operate func(x, y int) int
func 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 main
import (
"fmt"
"reflect"
)
func main() {
var f = func(str string) {
fmt.Println("hello", str)
}
fmt.Printf("������������%v\n", reflect.ValueOf(f).Kind())
f("func type")
}

���������������������

������������ func
hello func type

���������������������������������������������������������������������������

package main
import (
"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)
}

���������������

������������ func
hello ������������������

���������������������������������������������������������������exec ������������������������������������

上一篇:Go 学习笔记(65)— Go 中函数参数是传值还是传引用
下一篇:fatal error all goroutines are asleep - deadlock!

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月04日 19时31分48秒