首頁  >  文章  >  後端開發  >  Golang 裝飾器函數的哪些參數

Golang 裝飾器函數的哪些參數

WBOY
WBOY轉載
2024-02-08 23:09:391417瀏覽

Golang 装饰器函数的哪些参数

php小編百草為大家介紹Golang裝飾器函數的參數。在Golang中,裝飾器函數是一種特殊的函數,可以用來包裝其他函數,為其添加額外的功能。裝飾器函數通常有三個參數:原始函數、裝飾器函數的參數和傳回值。原始函數是需要被裝飾的函數,裝飾器函數的參數可以是任意類型,可以用來傳遞額外的參數給裝飾器函數,而返回值通常是一個函數,該函數會取代原始函數的執行。透過這些參數,我們可以實現各種靈活的裝飾器模式,為函數添加不同的功能,提高程式碼的可重複使用性和可擴充性。

問題內容

我想在 adminapi 的幾個方法上使用設定器,例如 update。為此,我創建了一個可以與其他方法匹配的方法類型。

我應該使用介面而不是 func type 嗎?

type adminapi struct {
}

type toadminctx func(ctx context.context, req interface{}) (interface{}, error)

func (a adminapi) adminm2msetter(s toadminctx) toadminctx {
    return func(ctx context.context, arg interface{}) (interface{}, error) {
        m2mprincipal, _ := a.getm2mprincipal(ctx)
        ctxm2m := extlib.setprincipal(ctx, m2mprincipal)
        return s(ctxm2m, arg)
    }
}

func (a adminapi) update(ctx context.context, req *reqtype) (resptype, error) {}
updateWithAdminCtx := a.adminAPI.AdminM2MSetter(s.adminAPI.Update)
// ERROR => cannot use s.adminAPI.Update (value of type func(ctx 
// context.Context, req *ReqType) (RespType, error)) as grpcAdmin.ToGetAdminCtx value in 
// argument to s.adminAPI.AdminM2MSetter

_, err := updateWithAdminCtx(ctx context.Context, req *ReqType)

解決方法

我認為您遇到的錯誤是不言自明的:

a.adminapi.adminm2msetter(s.adminapi.update)

正在通話

func (a adminapi) adminm2msetter(s toadminctx) toadminctx {

傳入 s.adminapi.update 作為參數,預期類型為 toadminctx。您的該類型定義為:

type toadminctx func(ctx context.context, req interface{}) (interface{}, error)

但是您的update 函數的第二個參數是*reqtype#,其第一個傳回值是resptype 值,因此update 不是toadminctxtoadminctx 函數類型是可以使用上下文和字面上的任何類型呼叫的函數。您的 update 函數不能保證在 toadminctx 函數可以的所有情況下都能運作。

您正在尋找的是一種「包裝」任何函數的方法,添加對 ctx 參數做一些工作(可能會設定一些值),然後傳遞呼叫。在 go 1.19 之前,我們透過添加某種包裝類型來做到這一點,如下所示:

type wrapper struct {
    updatereqtype *reqtype
    anothertype *reqtype2 // for some other call you want to wrap
}

更改所有相關函數,例如 update 函數以採用包裝器參數類型:

func (a adminapi) update(ctx context.context, req wrapper) (resp, error) {
    realreq := req.updatereqtype // get the actual request used here
}

回應類型將被類似地包裝和/或組合。

現在,go 支援泛型,在這種情況下它們非常有用,讓我們將 adminm2msetter 函數更改為如下所示:

func adminm2msetter[t any, r any](s func(context.context, t) (r, error)) func(context.context, t) (r, error) {
    return func (ctx context.context, arg t) (r, error) {
        m2mprincipal, _ := a.getm2mprincipal(ctx)
        ctxm2m := extlib.setprincipal(ctx, m2mprincipal)
        return s(ctxm2m, arg)
    }
}

這樣,我們只需要定義這個函數一次,而是依靠編譯器為我們需要的所有類型產生一個量身定制的函數。對於 update 函數,我們會執行以下操作:

a.adminapi.adminm2msetter[*reqtype, resptype](s.adminapi.update)

本質上是用 update 函數使用的特定類型來取代通用 tr 類型。因為我真的不知道你想以這種方式包裝什麼函數,所以我使用了t any, r any#,但是因為在我看來你正在嘗試包裝某種類型的請求處理程序,所以你可以創建自己的約束:

type Requests interface {
    *ReqType1 | *ReqType2 | *ReqType3 // and so on
}
type Responses interface {
    Resp1 | Resp2 | Resp3
}

只要將 [t any, r any] 替換為 [t requests, r responses]

以上是Golang 裝飾器函數的哪些參數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除