golang中函数类型
今天看Martini文档,其功能列表提到完全兼容 http.HandlerFunc 接口,就去查阅了 Go: net/http 的文档,看到type HandlerFunc这部分,顿时蒙圈了。由于之前学习的时候没有关注过function types的知识点,就Google了一些文章,才算是有了个大概的了解。 从golang的官方文档得知 function types 的解释是这样的。 A function type denotes the set of all functions with the same parameter and result types. 先找个例子来看一下: package main import "fmt" // Greeting function types type Greeting func(name string) string func say(g Greeting, n string) { fmt.Println(g(n)) } func english(name string) string { return "Hello, " + name } func main() { say(english, "World") } 输出 Hello, World say()函数要求传入一个Greeting类型