Go語言的修飾器編程

EricBarela 8年前發布 | 13K 次閱讀 Go語言 Google Go/Golang開發

之前寫過一篇《 Python修飾器的函數式編程 》,這種模式很容易的可以把一些函數裝配到另外一些函數上,可以讓你的代碼更為的簡單,也可以讓一些“小功能型”的代碼復用性更高,讓代碼中的函數可以像樂高玩具那樣自由地拼裝。所以,一直以來,我對修飾器decoration這種編程模式情有獨鐘,這里寫一篇Go語言相關的文章。

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

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

簡單示例

我們先來看一個示例:

package main

import "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 main

import (
  "fmt"
  "reflect"
  "runtime"
  "time"
)

type SumFunc func(int64, int64) int64

func 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 main

import (
        "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 main

import (
        "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.HandlerFunc

func 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) int
var myfoo MyFoo
Decorator(&myfoo, foo)
myfoo(1, 2, 3)

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

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

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

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

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

(全文完)

關注CoolShell微信公眾賬號可以在手機端搜索文章

 

來自:http://coolshell.cn/articles/17929.html

 

 本文由用戶 EricBarela 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!