首页 > 解决方案 > 与具有命名参数和未命名参数的函数的接口

问题描述

我正在学习 Go 中的 protobuf 和 gRPC。在生成 pb.go 文件时

protoc --go_out=plugins=grpc:chat chat.proto

对于文件chat.proto

syntax = "proto3";
package chat;

message Message {
  string body = 1;
}

service ChatService {
  rpc SayHello(Message) returns (Message) {}
}

生成的chat.pb.go有这两个接口:

type ChatServiceClient interface {
    SayHello(ctx context.Context, in *Message, opts ...grpc.CallOption) (*Message, error)
}
...
type ChatServiceServer interface {
    SayHello(context.Context, *Message) (*Message, error)
}

ChatServiceClient我对在界面中使用命名参数感到困惑。有没有使用这些参数ctxinopts。在这种情况下,我们什么时候应该使用命名参数和未命名参数?

标签: goprotocol-buffers

解决方案


参数名称是可选的,在接口的情况下,它可以纯粹用于文档目的。

规格:接口:

InterfaceType      = "interface" "{" { ( MethodSpec | InterfaceTypeName ) ";" } "}" .
MethodSpec         = MethodName Signature .

方法签名在哪里:

Signature      = Parameters [ Result ] .
Result         = Parameters | Type .
Parameters     = "(" [ ParameterList [ "," ] ] ")" .
ParameterList  = ParameterDecl { "," ParameterDecl } .
ParameterDecl  = [ IdentifierList ] [ "..." ] Type .

如您所见,IdentifierListinParameterDecl在方括号中,这意味着它是可选的。

想一个这样的例子:

type FileMover interface {
    MoveFile(dst, src string) error
}

它“响亮而清晰”。如果我们省略参数名称怎么办?

type FileMover interface {
    MoveFile(string, string) error
}

第一个参数是否标识源或目标并不明显。提供dstsrc命名文件,它使 thar 清楚。

当你实现一个接口并为一个方法提供实现时,如果你想引用参数,你必须给它们命名,因为你用它们的名字来引用它们,但是如果你不想引用参数, 即使这样它们也可能被省略。

请参阅相关问题:Go 中是否存在未命名的参数?


推荐阅读