go - 与具有命名参数和未命名参数的函数的接口
问题描述
我正在学习 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
我对在界面中使用命名参数感到困惑。有没有使用这些参数ctx
:in
和opts
。在这种情况下,我们什么时候应该使用命名参数和未命名参数?
解决方案
参数名称是可选的,在接口的情况下,它可以纯粹用于文档目的。
InterfaceType = "interface" "{" { ( MethodSpec | InterfaceTypeName ) ";" } "}" . MethodSpec = MethodName Signature .
方法签名在哪里:
Signature = Parameters [ Result ] . Result = Parameters | Type . Parameters = "(" [ ParameterList [ "," ] ] ")" . ParameterList = ParameterDecl { "," ParameterDecl } . ParameterDecl = [ IdentifierList ] [ "..." ] Type .
如您所见,IdentifierList
inParameterDecl
在方括号中,这意味着它是可选的。
想一个这样的例子:
type FileMover interface {
MoveFile(dst, src string) error
}
它“响亮而清晰”。如果我们省略参数名称怎么办?
type FileMover interface {
MoveFile(string, string) error
}
第一个参数是否标识源或目标并不明显。提供dst
和src
命名文件,它使 thar 清楚。
当你实现一个接口并为一个方法提供实现时,如果你想引用参数,你必须给它们命名,因为你用它们的名字来引用它们,但是如果你不想引用参数, 即使这样它们也可能被省略。
请参阅相关问题:Go 中是否存在未命名的参数?
推荐阅读
- c++ - QtConcurrent::run 作为非静态方法
- api - 如何在从 API 端点获取数据到谷歌表格时处理谷歌应用脚本 6 分钟的执行时间限制?
- xamarin.ios - Visual Studio 2019 要求确认 Apple 的 Apple ID 和隐私声明
- mysql - mysql 数据源抛出无法创建 PoolableConnectionFactory 异常,但旧的 jdbc 连接方法仍然有效
- node.js - 如何在反应中从firebase调用空集合或文档?
- r - 一个 R 函数是否可以像 [] 中那样不返回任何内容?
- sql - 如何从内部查询访问 GROUP BY 中的截断值?
- kubernetes - 如何引用在 DAG 外部创建的 sys.stdout 以在带有 withParam 的 DAG 内部使用?
- node.js - 纱线 - 错误:检索二进制文件时出错:未找到 - Solidity 项目
- excel - VBA & 团队:是否可以通过另一个工作簿中的宏打开团队中的 Excel 文件?