\u7b80\u5355\u793a\u4f8b<\/strong><\/div>\n\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n
package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc decorator(f func(s string)) func(s string) {\r\n\r\n return func(s string) {\r\n fmt.Println(\"Started\")\r\n f(s)\r\n fmt.Println(\"Done\")\r\n }\r\n}\r\n\r\nfunc Hello(s string) {\r\n fmt.Println(s)\r\n}\r\n\r\nfunc main() {\r\n decorator(Hello)(\"Hello, World!\")\r\n}<\/pre>\n\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u52a8\u7528\u4e86\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\u00a0decorator()<\/span>\uff0c\u5728\u8c03\u7528\u7684\u65f6\u5019\uff0c\u5148\u628a\u00a0Hello()<\/span>\u51fd\u6570\u4f20\u8fdb\u53bb\uff0c\u7136\u540e\u5176\u8fd4\u56de\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u8fd9\u4e2a\u533f\u540d\u51fd\u6570\u4e2d\u9664\u4e86\u8fd0\u884c\u4e86\u81ea\u5df1\u7684\u4ee3\u7801\uff0c\u4e5f\u8c03\u7528\u4e86\u88ab\u4f20\u5165\u7684\u00a0Hello()<\/span>\u00a0\u51fd\u6570\u3002<\/p>\n\u8fd9\u4e2a\u73a9\u6cd5\u548c Python \u7684\u5f02\u66f2\u540c\u5de5\uff0c\u53ea\u4e0d\u8fc7\uff0c\u6709\u4e9b\u9057\u61be\u7684\u662f\uff0cGo \u5e76\u4e0d\u652f\u6301\u50cf Python \u90a3\u6837\u7684\u00a0@decorator<\/span>\u00a0\u8bed\u6cd5\u7cd6\u3002\u6240\u4ee5\uff0c\u5728\u8c03\u7528\u4e0a\u6709\u4e9b\u96be\u770b\u3002\u5f53\u7136\uff0c\u5982\u679c\u4f60\u8981\u60f3\u8ba9\u4ee3\u7801\u5bb9\u6613\u8bfb\u4e00\u4e9b\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\uff1a<\/p>\nhello := decorator(Hello)\r\nhello(\"Hello\")<\/pre>\n\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e2a\u548c\u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4\u7684\u4f8b\u5b50\uff1a<\/p>\n
package main\r\n\r\nimport (\r\n \"fmt\"\r\n \"reflect\"\r\n \"runtime\"\r\n \"time\"\r\n)\r\n\r\ntype SumFunc func(int64, int64) int64\r\n\r\nfunc getFunctionName(i interface{}) string {\r\n return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()\r\n}\r\n\r\nfunc timedSumFunc(f SumFunc) SumFunc {\r\n return func(start, end int64) int64 {\r\n\r\n defer func(t time.Time) {\r\n fmt.Printf(\"--- Time Elapsed (%s): %v ---\/n\", \r\n getFunctionName(f), time.Since(t))\r\n }(time.Now())\r\n\r\n return f(start, end)\r\n }\r\n}\r\n\r\nfunc Sum1(start, end int64) int64 {\r\n var sum int64\r\n sum = 0\r\n if start > end {\r\n start, end = end, start\r\n }\r\n for i := start; i <= end; i++ {\r\n sum += i\r\n }\r\n return sum\r\n}\r\n\r\nfunc Sum2(start, end int64) int64 {\r\n if start > end {\r\n start, end = end, start\r\n }\r\n return (end - start + 1) * (end + start) \/ 2\r\n}\r\n\r\nfunc main() {\r\n\r\n sum1 := timedSumFunc(Sum1)\r\n sum2 := timedSumFunc(Sum2)\r\n\r\n fmt.Printf(\"%d, %d\/n\", sum1(-10000, 10000000), sum2(-10000, 10000000))\r\n}<\/pre>\n\u5173\u4e8e\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6709\u51e0\u4e2a\u4e8b\u8bf4\u660e\u4e00\u4e0b\uff1a<\/p>\n
1\uff09\u6709\u4e24\u4e2a Sum \u51fd\u6570\uff0cSum1()<\/span>\u00a0\u51fd\u6570\u5c31\u662f\u7b80\u5355\u7684\u505a\u4e2a\u5faa\u73af\uff0cSum2()<\/span>\u00a0\u51fd\u6570\u52a8\u7528\u4e86\u6570\u636e\u516c\u5f0f\u3002\uff08\u6ce8\u610f\uff1astart \u548c end \u6709\u53ef\u80fd\u6709\u8d1f\u6570\u7684\u60c5\u51b5\uff09<\/p>\n2\uff09\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86 Go \u8bed\u8a00\u7684\u53cd\u5c04\u673a\u5668\u6765\u83b7\u53d6\u51fd\u6570\u540d\u3002<\/p>\n
3\uff09\u4fee\u9970\u5668\u51fd\u6570\u662f\u00a0timedSumFunc()<\/span><\/p>\n\u8fd0\u884c\u540e\u8f93\u51fa\uff1a<\/p>\n
$ go run time.sum.go\r\n--- Time Elapsed (main.Sum1): 3.557469ms ---\r\n--- Time Elapsed (main.Sum2): 291ns ---\r\n49999954995000, 49999954995000<\/pre>\nHTTP \u76f8\u5173\u7684\u4e00\u4e2a\u793a\u4f8b<\/strong><\/div>\n\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e2a\u5904\u7406 HTTP \u8bf7\u6c42\u7684\u76f8\u5173\u7684\u4f8b\u5b50\u3002<\/p>\n
\u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684 HTTP Server \u7684\u4ee3\u7801\u3002<\/p>\n
package main\r\n\r\nimport (\r\n \"fmt\"\r\n \"log\"\r\n \"net\/http\"\r\n \"strings\"\r\n)\r\n\r\nfunc WithServerHeader(h http.HandlerFunc) http.HandlerFunc {\r\n return func(w http.ResponseWriter, r *http.Request) {\r\n log.Println(\"--->WithServerHeader()\")\r\n w.Header().Set(\"Server\", \"HelloServer v0.0.1\")\r\n h(w, r)\r\n }\r\n}\r\n\r\nfunc hello(w http.ResponseWriter, r *http.Request) {\r\n log.Printf(\"Recieved Request %s from %s\/n\", r.URL.Path, r.RemoteAddr)\r\n fmt.Fprintf(w, \"Hello, World! \"+r.URL.Path)\r\n}\r\n\r\nfunc main() {\r\n http.HandleFunc(\"\/v1\/hello\", WithServerHeader(hello))\r\n err := http.ListenAndServe(\":8080\", nil)\r\n if err != nil {\r\n log.Fatal(\"ListenAndServe: \", err)\r\n }\r\n}<\/pre>\n\n
\u4e0a\u9762\u4ee3\u7801\u4e2d\u4f7f\u7528\u5230\u4e86\u4fee\u9970\u6a21\u5f0f\uff0cWithServerHeader()<\/span>\u00a0\u51fd\u6570\u5c31\u662f\u4e00\u4e2a Decorator\uff0c\u5176\u4f20\u5165\u4e00\u4e2a\u00a0http.HandlerFunc<\/span>\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u6539\u5199\u7684\u7248\u672c\u3002\u4e0a\u9762\u7684\u4f8b\u5b50\u8fd8\u662f\u6bd4\u8f83\u7b80\u5355\uff0c\u7528\u00a0WithServerHeader()<\/span>\u00a0\u5c31\u53ef\u4ee5\u52a0\u5165\u4e00\u4e2a Response \u7684 Header\u3002<\/p>\n\u4e8e\u662f\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u6211\u4eec\u53ef\u4ee5\u5199\u51fa\u597d\u4e9b\u4e2a\u3002\u5982\u4e0b\u6240\u793a\uff0c\u6709\u5199 HTTP \u54cd\u5e94\u5934\u7684\uff0c\u6709\u5199\u8ba4\u8bc1 Cookie \u7684\uff0c\u6709\u68c0\u67e5\u8ba4\u8bc1Cookie\u7684\uff0c\u6709\u6253\u65e5\u5fd7\u7684\u2026\u2026<\/p>\n
package main\r\n\r\nimport (\r\n \"fmt\"\r\n \"log\"\r\n \"net\/http\"\r\n \"strings\"\r\n)\r\n\r\nfunc WithServerHeader(h http.HandlerFunc) http.HandlerFunc {\r\n return func(w http.ResponseWriter, r *http.Request) {\r\n log.Println(\"--->WithServerHeader()\")\r\n w.Header().Set(\"Server\", \"HelloServer v0.0.1\")\r\n h(w, r)\r\n }\r\n}\r\n\r\nfunc WithAuthCookie(h http.HandlerFunc) http.HandlerFunc {\r\n return func(w http.ResponseWriter, r *http.Request) {\r\n log.Println(\"--->WithAuthCookie()\")\r\n cookie := &http.Cookie{Name: \"Auth\", Value: \"Pass\", Path: \"\/\"}\r\n http.SetCookie(w, cookie)\r\n h(w, r)\r\n }\r\n}\r\n\r\nfunc WithBasicAuth(h http.HandlerFunc) http.HandlerFunc {\r\n return func(w http.ResponseWriter, r *http.Request) {\r\n log.Println(\"--->WithBasicAuth()\")\r\n cookie, err := r.Cookie(\"Auth\")\r\n if err != nil || cookie.Value != \"Pass\" {\r\n w.WriteHeader(http.StatusForbidden)\r\n return\r\n }\r\n h(w, r)\r\n }\r\n}\r\n\r\nfunc WithDebugLog(h http.HandlerFunc) http.HandlerFunc {\r\n return func(w http.ResponseWriter, r *http.Request) {\r\n log.Println(\"--->WithDebugLog\")\r\n r.ParseForm()\r\n log.Println(r.Form)\r\n log.Println(\"path\", r.URL.Path)\r\n log.Println(\"scheme\", r.URL.Scheme)\r\n log.Println(r.Form[\"url_long\"])\r\n for k, v := range r.Form {\r\n log.Println(\"key:\", k)\r\n log.Println(\"val:\", strings.Join(v, \"\"))\r\n }\r\n h(w, r)\r\n }\r\n}\r\nfunc hello(w http.ResponseWriter, r *http.Request) {\r\n log.Printf(\"Recieved Request %s from %s\/n\", r.URL.Path, r.RemoteAddr)\r\n fmt.Fprintf(w, \"Hello, World! \"+r.URL.Path)\r\n}\r\n\r\nfunc main() {\r\n http.HandleFunc(\"\/v1\/hello\", WithServerHeader(WithAuthCookie(hello)))\r\n http.HandleFunc(\"\/v2\/hello\", WithServerHeader(WithBasicAuth(hello)))\r\n http.HandleFunc(\"\/v3\/hello\", WithServerHeader(WithBasicAuth(WithDebugLog(hello))))\r\n err := http.ListenAndServe(\":8080\", nil)\r\n if err != nil {\r\n log.Fatal(\"ListenAndServe: \", err)\r\n }\r\n}<\/pre>\n<\/div>\n