Golang 修飾器編程

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
導讀 之前寫過一篇《Python修飾器的函數式編程》,這種模式很容易的可以把一些函數裝配到另外一些函數上,可以讓你的代碼更為的簡單,也可以讓一些“小功能型”的代碼複用性更高,讓代碼中的函數可以像樂高玩具那樣自由地拼裝。所以,一直以來,我對修飾器decoration這種編程模式情有獨鐘,這裡寫一篇Go語言相關的文章。

看過Python修飾器那篇文章的同學,一定知道這是一種函數式編程的玩法——用一個高階函數來封裝一下。多嘮叨一句,關於函數式編程,可以參看我之前寫過一篇文章《函數式編程》,這篇文章主要是,想通過從過程式編程的思維方式過渡到函數式編程的思維方式,從而帶動更多的人玩函數式編程,所以,如果你想瞭解一下函數式編程,那麼可以移步先閱讀一下。所以,Go語言的修飾器編程模式,其實也就是函數式編程的模式。

不過,要提醒注意的是,Go 語言的“糖”不多,而且又是強型別的靜態無虛擬機器的語言,所以,無法做到像 Java 和 Python 那樣的優雅的修飾器的代碼。當然,也許是我才才疏學淺,如果你知道有更多的寫法,請你一定告訴我。先謝過了。

簡單樣本

我們先來看一個樣本:

package mainimport "fmt"func decorator(f func(s string)) func(s string) {        return func(s string) {                fmt.Println("Started")                f(s)                fmt.Println("Done")        }}func Hello(s string) {        fmt.Println(s)}func main() {        decorator(Hello)("Hello, World!")}

我們可以看到,我們動用了一個高階函數 decorator(),在調用的時候,先把 Hello()函數傳進去,然後其返回一個匿名函數,這個匿名函數中除了運行了自己的代碼,也調用了被傳入的 Hello() 函數。

這個玩法和 Python 的異曲同工,只不過,有些遺憾的是,Go 並不支援像 Python 那樣的 @decorator 文法糖。所以,在調用上有些難看。當然,如果你要想讓代碼容易讀一些,你可以這樣:

hello := decorator(Hello)hello("Hello")

我們再來看一個和計算已耗用時間的例子:

package mainimport (  "fmt"  "reflect"  "runtime"  "time")type SumFunc func(int64, int64) int64func getFunctionName(i interface{}) string {  return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()}func timedSumFunc(f SumFunc) SumFunc {  return func(start, end int64) int64 {    defer func(t time.Time) {      fmt.Printf("--- Time Elapsed (%s): %v ---/n",           getFunctionName(f), time.Since(t))    }(time.Now())    return f(start, end)  }}func Sum1(start, end int64) int64 {  var sum int64  sum = 0  if start > end {    start, end = end, start  }  for i := start; i <= end; i++ {    sum += i  }  return sum}func Sum2(start, end int64) int64 {  if start > end {    start, end = end, start  }  return (end - start + 1) * (end + start) / 2}func main() {  sum1 := timedSumFunc(Sum1)  sum2 := timedSumFunc(Sum2)  fmt.Printf("%d, %d/n", sum1(-10000, 10000000), sum2(-10000, 10000000))}

關於上面的代碼,有幾個事說明一下:

1)有兩個 Sum 函數,Sum1() 函數就是簡單的做個迴圈,Sum2() 函數動用了資料公式。(注意:start 和 end 有可能有負數的情況)

2)代碼中使用了 Go 語言的反射機器來擷取函數名。

3)修飾器函數是 timedSumFunc()

運行後輸出:

$ go run time.sum.go--- Time Elapsed (main.Sum1): 3.557469ms ------ Time Elapsed (main.Sum2): 291ns ---49999954995000, 49999954995000

HTTP 相關的一個樣本

我們再來看一個處理 HTTP 要求的相關的例子。

先看一個簡單的 HTTP Server 的代碼。

package mainimport (        "fmt"        "log"        "net/http"        "strings")func WithServerHeader(h http.HandlerFunc) http.HandlerFunc {        return func(w http.ResponseWriter, r *http.Request) {                log.Println("--->WithServerHeader()")                w.Header().Set("Server", "HelloServer v0.0.1")                h(w, r)        }}func hello(w http.ResponseWriter, r *http.Request) {        log.Printf("Recieved Request %s from %s/n", r.URL.Path, r.RemoteAddr)        fmt.Fprintf(w, "Hello, World! "+r.URL.Path)}func main() {        http.HandleFunc("/v1/hello", WithServerHeader(hello))        err := http.ListenAndServe(":8080", nil)        if err != nil {                log.Fatal("ListenAndServe: ", err)        }}

上面代碼中使用到了修飾模式,WithServerHeader() 函數就是一個 Decorator,其傳入一個 http.HandlerFunc,然後返回一個改寫的版本。上面的例子還是比較簡單,用 WithServerHeader() 就可以加入一個 Response 的 Header。

於是,這樣的函數我們可以寫出好些個。如下所示,有寫 HTTP 回應標頭的,有寫認證 Cookie 的,有檢查認證Cookie的,有打日誌的……

package mainimport (        "fmt"        "log"        "net/http"        "strings")func WithServerHeader(h http.HandlerFunc) http.HandlerFunc {        return func(w http.ResponseWriter, r *http.Request) {                log.Println("--->WithServerHeader()")                w.Header().Set("Server", "HelloServer v0.0.1")                h(w, r)        }}func WithAuthCookie(h http.HandlerFunc) http.HandlerFunc {        return func(w http.ResponseWriter, r *http.Request) {                log.Println("--->WithAuthCookie()")                cookie := &http.Cookie{Name: "Auth", Value: "Pass", Path: "/"}                http.SetCookie(w, cookie)                h(w, r)        }}func WithBasicAuth(h http.HandlerFunc) http.HandlerFunc {        return func(w http.ResponseWriter, r *http.Request) {                log.Println("--->WithBasicAuth()")                cookie, err := r.Cookie("Auth")                if err != nil || cookie.Value != "Pass" {                        w.WriteHeader(http.StatusForbidden)                        return                }                h(w, r)        }}func WithDebugLog(h http.HandlerFunc) http.HandlerFunc {        return func(w http.ResponseWriter, r *http.Request) {                log.Println("--->WithDebugLog")                r.ParseForm()                log.Println(r.Form)                log.Println("path", r.URL.Path)                log.Println("scheme", r.URL.Scheme)                log.Println(r.Form["url_long"])                for k, v := range r.Form {                        log.Println("key:", k)                        log.Println("val:", strings.Join(v, ""))                }                h(w, r)        }}func hello(w http.ResponseWriter, r *http.Request) {        log.Printf("Recieved Request %s from %s/n", r.URL.Path, r.RemoteAddr)        fmt.Fprintf(w, "Hello, World! "+r.URL.Path)}func main() {        http.HandleFunc("/v1/hello", WithServerHeader(WithAuthCookie(hello)))        http.HandleFunc("/v2/hello", WithServerHeader(WithBasicAuth(hello)))        http.HandleFunc("/v3/hello", WithServerHeader(WithBasicAuth(WithDebugLog(hello))))        err := http.ListenAndServe(":8080", nil)        if err != nil {                log.Fatal("ListenAndServe: ", err)        }}

多個修飾器的 Pipeline

在使用上,需要對函數一層層的套起來,看上去好像不是很好看,如果需要 decorator 比較多的話,代碼會比較難看了。嗯,我們可以重構一下。

重構時,我們需要先寫一個工具函數——用來遍曆並調用各個 decorator:

type HttpHandlerDecorator func(http.HandlerFunc) http.HandlerFuncfunc Handler(h http.HandlerFunc, decors ...HttpHandlerDecorator) http.HandlerFunc {        for i := range decors {                d := decors[len(decors)-1-i] // iterate in reverse                h = d(h)        }        return h}

然後,我們就可以像下面這樣使用了。

http.HandleFunc("/v4/hello", Handler(hello,                WithServerHeader, WithBasicAuth, WithDebugLog))

這樣的代碼是不是更易讀了一些?pipeline 的功能也就出來了。

泛型的修飾器

不過,對於 Go 的修飾器模式,還有一個小問題 —— 好像無法做到泛型,就像上面那個計算時間的函數一樣,其代碼耦合了需要被修飾的函數的介面類型,無法做到非常通用,如果這個事解決不了,那麼,這個修飾器模式還是有點不好用的。

因為 Go 語言不像 Python 和 Java,Python是動態語言,而 Java 有語言虛擬機器,所以他們可以幹好些比較變態的事,然而 Go 語言是一個靜態語言,這意味著其類型需要在編譯時間就要搞定,否則無法編譯。不過,Go 語言支援的最大的泛型是 interface{} 還有比較簡單的 reflection 機制,在上面做做文章,應該還是可以搞定的。

廢話不說,下面是我用 reflection 機制寫的一個比較通用的修飾器(為了便於閱讀,我刪除了出錯判斷代碼)

func Decorator(decoPtr, fn interface{}) (err error) {        var decoratedFunc, targetFunc reflect.Value        decoratedFunc = reflect.ValueOf(decoPtr).Elem()        targetFunc = reflect.ValueOf(fn)        v := reflect.MakeFunc(targetFunc.Type(),                func(in []reflect.Value) (out []reflect.Value) {                        fmt.Println("before")                        out = targetFunc.Call(in)                        fmt.Println("after")                        return                })        decoratedFunc.Set(v)        return}

上面的代碼動用了 reflect.MakeFunc() 函數制出了一個新的函數其中的 targetFunc.Call(in) 調用了被修飾的函數。關於 Go 語言的反射機制,推薦官方文章 —— 《The Laws of Reflection》,在這裡我不多說了。

上面這個 Decorator() 需要兩個參數,

  • 第一個是出參 decoPtr ,就是完成修飾後的函數
  • 第二個是入參 fn ,就是需要修飾的函數

這樣寫是不是有些二?的確是的。不過,這是我個人在 Go 語言裡所能寫出來的最好的的代碼了。如果你知道更多優雅的,請你一定告訴我!

好的,讓我們來看一下使用效果。首先假設我們有兩個需要修飾的函數:

func foo(a, b, c int) int {        fmt.Printf("%d, %d, %d /n", a, b, c)        return a + b + c}func bar(a, b string) string {        fmt.Printf("%s, %s /n", a, b)        return a + b}

然後,我們可以這樣做:

type MyFoo func(int, int, int) intvar myfoo MyFooDecorator(&myfoo, foo)myfoo(1, 2, 3)

你會發現,使用 Decorator() 時,還需要先聲明一個函數簽名,感覺好傻啊。一點都不泛型,不是嗎?

嗯。如果你不想聲明函數簽名,那麼你也可以這樣

mybar := barDecorator(&mybar, bar)mybar("hello,", "world!")

好吧,看上去不是那麼的漂亮,但是 it works。看樣子 Go 語言目前本身的特性無法做成像 Java 或 Python 那樣,對此,我們只能多求 Go 語言多放糖了!

Again, 如果你有更好的寫法,請你一定要告訴我。

原文來自:http://www.linuxprobe.com/go-modifier-programming.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.