Why can't I use the literal func as a custom func type despite the parameter mapping in golang?

The package google.golang.org/grpc

defines the 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 would 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 am getting this error:

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

Looking at Why can I inject alias functions and use them without being cast? I am under the impression that the anonymous function returned in mine clientInterceptor()

should match grpc.ClientInterceptor

.

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

Two types of functions are identical if they have the same number of parameters and result values, the corresponding parameters and result types are identical, and both functions are variables or not. The parameter and result names are not necessarily the same.

I've tried casting and creating type variables grpc.UnaryClientInterceptor

but nothing seems to work.

I also did pretty much the same with grpc.UnaryServerInterceptor

and had no problem.

What am I missing here?

+3


source to share


1 answer


You can reference a package context

differently than your version grpc

. Since version 1.7, the package has moved from golang.org/x/net/context

only to context

, and the compiler will most likely not see them as equivalent.



+5


source







All Articles