What means foo()(bar) in go and how it works?

huangapple go评论84阅读模式
英文:

What means foo()(bar) in go and how it works?

问题

auth0的Golang快速入门教程中,我找到了这段代码:

router.Handle("/api/private", middleware.EnsureValidToken()(
    http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        w.WriteHeader(http.StatusOK)
        w.Write([]byte(`{"message":"Hello from a private endpoint! You need to be authenticated to see this."}`))
    }),
))

然后我将其简化为以下形式:

func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(http.StatusOK)
    w.Write([]byte(`{"message":"Hello from test"}`))
}

func preHandler() func(next http.Handler) http.Handler {
    log.Println("in preHandler")
    return func(next http.Handler) http.Handler {
        return check(next)
    }
}

func main() {
    http.Handle("/test/", preHandler()(http.HandlerFunc(handler)))
    http.ListenAndServe(":80", nil)
}

但是我无法理解这段代码preHandler()(http.HandlerFunc(handler))的工作原理和含义是什么。

谢谢帮助!

附:
我尝试在谷歌上找到答案,但没有找到。我只是想弄清楚它是如何工作的。

英文:

In auth0 QuickStart tutorial for Golang I had found this piece of code:

router.Handle("/api/private", middleware.EnsureValidToken()(
    http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        w.WriteHeader(http.StatusOK)
        w.Write([]byte(`{"message":"Hello from a private endpoint! You need to be authenticated to see this."}`))
    }),
 ))

Then I had simplify it to this form:

func handler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	w.Write([]byte(`{"message":"Hello from test"}`))
}

func preHandler() func(next http.Handler) http.Handler {

	log.Println("in preHandler")
	return func(next http.Handler) http.Handler {
		return check(next)
	}
}

func main() {
	http.Handle("/test/", preHandler()(http.HandlerFunc(handler)))
	http.ListenAndServe(":80", nil)
}

But I can't figure out how is working and what is meaning of this piece of code preHandler()(http.HandlerFunc(handler))

Thnx for help!

P.s.
I tried to find answer in google but nothing. I just want to find out how it works

答案1

得分: 3

这个语句

http.Handle("/test/", preHandler()(http.HandlerFunc(handler)))

包含了三个函数调用和一个类型转换。我将把这个语句拆分成四个语句来解释:

f := preHandler() // f 是一个类型为 func(next http.Handler) http.Handler 的函数
h := http.HandlerFunc(handler) // 类型转换为 http.HandlerFunc
hw := f(h)   // 调用 f 并传入 h,结果是一个 http.Handler
http.Handle("/test/", hw)  // 注册这个处理器
英文:

The statement

http.Handle("/test/", preHandler()(http.HandlerFunc(handler)))

has three function calls and a type conversion. I'll explain what's going on by splitting the one statement into four statements:

f := preHandler() // f is function with type func(next http.Handler) http.Handler
h := http.HandlerFunc(handler) // type conversion to http.HandlerFunc
hw := f(h)   // call f with h, result is an http.Handler
http.Handle("/test/", hw)  // register the handler

huangapple
  • 本文由 发表于 2022年12月10日 00:41:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/74745922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定