Usage analysis of type func() in golang

When looking at the code of the http service part of golang, I was puzzled by the type func() in golang, and I didn't understand the code for a while. After checking the information, I got a little understanding.
In golang, an http service can be implemented simply like this

package main

import "net/http"

func mHttp() {
    http.HandleFunc("/", h)
    http.ListenAndServe("0.0.0.0:8888",nil)
}
func h(w http.ResponseWriter, r *http.Request) {

}

http.HandleFunc()It is a registration function, passing a string type route, and a function, the parameter of the function is (http.ResponseWriter, *http.Request) . Track entry function, in golang source code net/http/server.go file

func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
    DefaultServeMux.HandleFunc(pattern, handler)
}

Called in HandleFuncDefaultServeMux.HandleFunc(pattern, handler)
As for the purpose of these functions, let's not discuss it. This is not the focus of this article.
Follow up the function again

func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
    if handler == nil {
        panic("http: nil handler")
    }
    mux.Handle(pattern, HandlerFunc(handler))
}

What the hell is the second parameter in mux.Handle(pattern, HandlerFunc(handler))HandlerFunc(handler) .
Follow up

type HandlerFunc func(ResponseWriter, *Request)

func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
    f(w, r)
}

It turns out that HandlerFunc is a function defined by type , and the type of the function is the type originally passed in. func(ResponseWriter, *Request)
ServeHTTP is a method of HandlerFunc (note that methods and functions in golang are not the same thing). And HandlerFunc implements the Handler interface
Handler interface definition:

type Handler interface {
    ServeHTTP(ResponseWriter, *Request)
}

Going back to the HandleFunc method, mux.Handle(pattern, HandlerFunc(handler))the second parameter is to force the incoming function handler to the HandlerFunc type, so that the handler implements the Handler interface.
At this point, we understand that it HandlerFunc(handler)is to force ordinary functions into type-defined functions.
Now write a simple demo to verify:

package main

import "fmt"

func main() {
   one(2, callback)
}

//需要传递函数
func callback(i int) {
   fmt.Println("i am callBack")
   fmt.Println(i)
}

//main中调用的函数
func one(i int, f func(int)) {
   two(i, fun(f))
}

//one()中调用的函数
func two(i int, c Call) {
   c.call(i)
}

//定义的type函数
type fun func(int)

//fun实现的Call接口的call()函数
func (f fun) call(i int) {
   f(i)
}

//接口
type Call interface {
   call(int)
}

First look at the results of the program:


We called the one() function in the main() function , passed the callback() function, and finally called the callback() function we passed in .

 

Think about it:

Use the type definition function func(int) to
define the Call interface. There is a function call(int)
in Call that calls one(2, callback) in main () , calls two() in one() , and passes in the two() function Previously, type conversion was implemented for the callback function, from an ordinary function to a function defined by type. Call the incoming c in two() because c implements the Call interface, so you can call the call() function, and finally call the callback() function we passed in .

Guess you like

Origin blog.csdn.net/qq_32907195/article/details/112189207