首页 > 后端开发 > Golang > 正文

Golang 装饰器函数的哪些参数

WBOY
发布: 2024-02-08 23:09:39
转载
1416 人浏览过

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 函数的第二个参数是 *reqtype,其第一个返回值是 resptype 值,因此

不是 toadminctxtoadminctx 函数类型是可以使用上下文和字面上的任何类型调用的函数。您的

函数不能保证在 toadminctx 函数可以的所有情况下都能工作。ctx

您正在寻找的是一种“包装”任何函数的方法,添加对

参数做一些工作(可能设置一些值),然后传递调用。在 go 1.19 之前,我们通过添加某种包装类型来做到这一点,如下所示:update

type wrapper struct {
    updatereqtype *reqtype
    anothertype *reqtype2 // for some other call you want to wrap
}
登录后复制

更改所有相关函数,例如

函数以采用包装器参数类型:

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

响应类型将被类似地包装和/或组合。adminm2msetter

现在,go 支持泛型,在这种情况下它们非常有用,让我们将

函数更改为如下所示:update

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 函数使用的特定类型替换通用 tr 类型。因为我真的不知道你想以这种方式包装什么函数,所以我使用了 t any, r any

a.adminapi.adminm2msetter[*reqtype, resptype](s.adminapi.update)
登录后复制

本质上是用

函数使用的特定类型替换通用 tr 类型。因为我真的不知道你想以这种方式包装什么函数,所以我使用了 t any, r any,但是因为在我看来你正在尝试包装某种类型的请求处理程序,所以你可以创建自己的约束:[t any, r any] 替换为 [t requests, r responses]

type Requests interface {
    *ReqType1 | *ReqType2 | *ReqType3 // and so on
}
type Responses interface {
    Resp1 | Resp2 | Resp3
}
登录后复制
只需将 🎜🎜

以上是Golang 装饰器函数的哪些参数的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!