In golang function type

Martini today to see the document, its feature list mentioned fully compatible http.HandlerFuncinterfaces, went to view the Go: net / http document, see the type HandlerFunc this part, Mongolia suddenly turns up. Because of the time previously learned knowledge no concern over function types, and some articles on Google, be considered with a rough idea.

Golang learned from official documentation of function types explanation is this.

A function type denotes the set of all functions with the same parameter and result types.

First find an example look at:

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 () function expects to be passed a Greeting type, because the parameters and return value of the function english Greeting like, refer to the concept of interface type conversion can be done here. We have another way to achieve the above functions:

package main

import "fmt"

// Greeting function types
type Greeting func(name string) string

func (g Greeting) say(n string) {
    fmt.Println(g(n))
}

func english(name string) string {
    return "Hello, " + name
}

func main() {
    g := Greeting(english)
    g.say("World")
}

 

同样输出Hello, World,只是给Greeting类型添加了say()方法。上面说了,函数类型是表示所有包含相同参数和返回类型的函数集合。我们在一开始先把func(name string) string这样的函数声明成Greeting类型,接着我们通过Greeting(english)english函数转换成Greeting类型。通过这个转换以后,我们就可以借由变量g调用Greeting类型的say()方法。两段代码的差异就是go的类型系统添加方法和类C++语言添加类型方法的差异,具体讲解可以去查看《Go语言编程》第3章为类型添加方法这一节。

既然是函数集合,那么只有一个函数显然是不足以说明问题的。

package main

import "fmt"

// Greeting function types
type Greeting func(name string) string

func (g Greeting) say(n string) {
    fmt.Println(g(n))
}

func english(name string) string {
    return "Hello, " + name
}

func french(name string) string {
    return "Bonjour, " + name
}

func main() {
    g := Greeting(english)
    g.say("World")
    g = Greeting(french)
    g.say("World")
}
输出

Hello, World
Bonjour, World

 

在其他语言里面,有些函数可以直接作为参数传递,有些是以函数指针进行传递,但是都没有办法像go这样可以给函数类型“增加”新方法。

回到Go: net/http的HandlerFunc类型,只要Martini的函数遵循文档中type HandlerFunc func(ResponseWriter, *Request)的要求,就可以转换成HandlerFunc类型,也就可以调用func (HandlerFunc)ServeHTTP函数。

 

Guess you like

Origin www.cnblogs.com/igoodful/p/11519695.html