为了账号安全,请及时绑定邮箱和手机立即绑定

与具有命名参数和未命名参数的函数的接口

与具有命名参数和未命名参数的函数的接口

Go
眼眸繁星 2022-07-25 10:17:45
我正在学习 Go 中的 protobuf 和 gRPC。在生成 pb.go 文件时protoc --go_out=plugins=grpc:chat chat.proto对于文件chat.protosyntax = "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我对在界面中使用命名参数感到困惑。有没有使用这些参数ctx:in和opts。在这种情况下,我们什么时候应该命名参数和未命名参数?
查看完整描述

1 回答

?
饮歌长啸

TA贡献1951条经验 获得超3个赞

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


规格:接口:


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

}

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


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


查看完整回答
反对 回复 2022-07-25
  • 1 回答
  • 0 关注
  • 79 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号