英文:
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
你可能在使用grpc
的context
包时与你的版本不同。从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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论