Why can I not use func literal as custom func type despite matching parameters in golang?

huangapple go评论96阅读模式
英文:

Why can I not use func literal as custom func type despite matching parameters in golang?

问题

google.golang.org/grpc定义了类型UnaryClientInterceptor,如下所示:

type UnaryClientInterceptor func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error

在我的代码中,我想要做如下操作:

func clientInterceptor() grpc.UnaryClientInterceptor {
    return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
        // 拦截处理
        return invoker(ctx, method, req, reply, cc, opts...)
    }
}

然而,我遇到了以下错误:

"cannot use func literal (type func("context".Context, string, interface {}, interface {}, *grpc.ClientConn, grpc.UnaryInvoker, ...grpc.CallOption) error) as type grpc.UnaryClientInterceptor in return argument"

根据https://stackoverflow.com/questions/19334542/why-can-i-type-alias-functions-and-use-them-without-casting中的解释,我得到的印象是clientInterceptor()返回的匿名函数应该与grpc.ClientInterceptor类型匹配。

此外,根据类型标识的规范(http://golang.org/ref/spec#Type_identity):

如果两个函数类型具有相同数量的参数和结果值,对应的参数和结果类型相同,并且两个函数都是可变参数或都不是,则它们是相同的。参数和结果的名称不需要匹配。

我尝试过进行类型转换和创建grpc.UnaryClientInterceptor类型的变量,但都没有成功。

我还以基本相同的方式处理了grpc.UnaryServerInterceptor,并没有遇到任何问题。

我在这里漏掉了什么?

英文:

Package google.golang.org/grpc defines type UnaryClientInterceptor as:

type UnaryClientInterceptor func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error

In my code I'd like to do something like:

func clientInterceptor() grpc.UnaryClientInterceptor {
    return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
        //intercept stuff
        return invoker(ctx, method, req, reply, cc, opts...)
    }
}

However I get this error:

"cannot use func literal (type func("context".Context, string, interface {}, interface {}, *grpc.ClientConn, grpc.UnaryInvoker, ...grpc.CallOption) error) as type grpc.UnaryClientInterceptor in return argument"

Looking at https://stackoverflow.com/questions/19334542/why-can-i-type-alias-functions-and-use-them-without-casting I get the impression that the anonymous function returned in my clientInterceptor() should match the grpc.ClientInterceptor type.

Also, from the spec on Type Identity (http://golang.org/ref/spec#Type_identity)

> Two function types are identical if they have the same number of parameters and result values, corresponding parameter and result types are identical, and either both functions are variadic or neither is. Parameter and result names are not required to match.

I've tried casting and making variables of type grpc.UnaryClientInterceptor but nothing works.

I also did essentially the exact same thing with the grpc.UnaryServerInterceptor and had no problems.

What am I missing here?

答案1

得分: 8

你可能在使用grpccontext包时与你的版本不同。从Go 1.7开始,该包已从golang.org/x/net/context移动到context,编译器可能不会将它们视为等效。

英文:

You may be referencing the context package differently than your version of grpc. As of Go 1.7, the package moved from golang.org/x/net/context to just context, and the compiler likely doesn't see them as equivalent.

huangapple
  • 本文由 发表于 2017年5月11日 03:30:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/43901235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定