具有命名参数和未命名参数的函数接口
Interface with function with named vs unnamed parameters
我正在学习 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
有这2个接口:
type ChatServiceClient interface {
SayHello(ctx context.Context, in *Message, opts ...grpc.CallOption) (*Message, error)
}
...
type ChatServiceServer interface {
SayHello(context.Context, *Message) (*Message, error)
}
我对 ChatServiceClient
界面中命名参数的使用感到困惑。这些参数是否有任何用途:ctx
、in
和 opts
。在这种情况下,我们什么时候应该命名参数与未命名参数?
参数名称是可选的,在接口的情况下,它可能纯粹出于文档目的而提供。
InterfaceType = "interface" "{" { ( MethodSpec | InterfaceTypeName ) ";" } "}" .
MethodSpec = MethodName Signature .
其中方法Signature是:
Signature = Parameters [ Result ] .
Result = Parameters | Type .
Parameters = "(" [ ParameterList [ "," ] ] ")" .
ParameterList = ParameterDecl { "," ParameterDecl } .
ParameterDecl = [ IdentifierList ] [ "..." ] Type .
如您所见,ParameterDecl
中的IdentifierList
在方括号中,表示它是可选的。
想想这样的例子:
type FileMover interface {
MoveFile(dst, src string) error
}
它“响亮而清晰”。如果我们省略参数名称怎么办?
type FileMover interface {
MoveFile(string, string) error
}
第一个参数标识源还是目标并不明显。提供 dst
和 src
名称 文档 ,它清楚地表明了这一点。
当你实现一个接口并提供一个方法的实现时,如果你想引用参数,你必须命名它们,因为你是通过它们的名字来引用它们,但如果你不想引用参数,即使这样也可以省略。
参见相关问题:
我正在学习 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
有这2个接口:
type ChatServiceClient interface {
SayHello(ctx context.Context, in *Message, opts ...grpc.CallOption) (*Message, error)
}
...
type ChatServiceServer interface {
SayHello(context.Context, *Message) (*Message, error)
}
我对 ChatServiceClient
界面中命名参数的使用感到困惑。这些参数是否有任何用途:ctx
、in
和 opts
。在这种情况下,我们什么时候应该命名参数与未命名参数?
参数名称是可选的,在接口的情况下,它可能纯粹出于文档目的而提供。
InterfaceType = "interface" "{" { ( MethodSpec | InterfaceTypeName ) ";" } "}" . MethodSpec = MethodName Signature .
其中方法Signature是:
Signature = Parameters [ Result ] . Result = Parameters | Type . Parameters = "(" [ ParameterList [ "," ] ] ")" . ParameterList = ParameterDecl { "," ParameterDecl } . ParameterDecl = [ IdentifierList ] [ "..." ] Type .
如您所见,ParameterDecl
中的IdentifierList
在方括号中,表示它是可选的。
想想这样的例子:
type FileMover interface {
MoveFile(dst, src string) error
}
它“响亮而清晰”。如果我们省略参数名称怎么办?
type FileMover interface {
MoveFile(string, string) error
}
第一个参数标识源还是目标并不明显。提供 dst
和 src
名称 文档 ,它清楚地表明了这一点。
当你实现一个接口并提供一个方法的实现时,如果你想引用参数,你必须命名它们,因为你是通过它们的名字来引用它们,但如果你不想引用参数,即使这样也可以省略。
参见相关问题: