我想了解 Go 中的高阶函数。我有以下程序:
package main
import "fmt"
func caller(foo func(string) string) {
result := foo("David")
fmt.Println(result)
}
bar := func(name string) string {
return "Hello " + name
}
func main(){
caller(bar)
}
当我运行它时:
去运行 higher_order_functions.go
它给出了以下错误:
# command-line-arguments
./higher_order_functions.go:10:1: syntax error: non-declaration statement outside function body
./higher_order_functions.go:11:5: syntax error: unexpected return, expecting )
我期待输出:
“你好大卫”
SMILET
鸿蒙传说
相关分类