如果一个字段在 Go 中是 "filtered" 是什么意思?
What does it mean if a field is "filtered" in Go?
在 Go 文档中,类型通常只显示导出的字段。例如,time.Timer 文档 (https://golang.org/pkg/time/#Timer) 显示如下:
type Timer
The Timer type represents a single event. When the Timer
expires, the current time will be sent on C, unless the Timer was
created by AfterFunc. A Timer must be created with NewTimer or
AfterFunc.
type Timer struct {
C <-chan Time
// contains filtered or unexported fields
}
Go 利用资本来区分导出字段和未导出字段,所以这一点很清楚。但是,包含 "filtered" 字段是什么意思(例如在上面评论的上下文中)?
那个comment
是由 go/printer
code based on the AST passed to it. Some of the AST nodes have a field that flags them as incomplete and this field is used by the printer to decide whether or not to print that comment. However the printer has no way of knowing the rules and reasons for why that field was set to true or false and so by convention it is assumend it was done by a filter, the most common one being exportFilter
产生的,因此是语言。
Incomplete
字段被导出并且可以被任何有权访问 AST 的东西设置为 true/false。您可以自己遍历 AST,将每个 Incomplete 字段设置为 true,同时保持节点完好无损,然后将 AST 传递给打印机,然后打印机将生成包含所有字段、已导出和未导出的结构,以及该注释。
Godoc filters the AST with ast.FileExports
which by default removes only unexported nodes, and then passes AST 到打印机。因此,在 Godoc 的情况下,该评论中的 "filtered" 与 "unexported".
同义
Playground link 来说明打印机的行为。
在 Go 文档中,类型通常只显示导出的字段。例如,time.Timer 文档 (https://golang.org/pkg/time/#Timer) 显示如下:
type Timer
The Timer type represents a single event. When the Timer expires, the current time will be sent on C, unless the Timer was created by AfterFunc. A Timer must be created with NewTimer or AfterFunc.
type Timer struct {
C <-chan Time
// contains filtered or unexported fields
}
Go 利用资本来区分导出字段和未导出字段,所以这一点很清楚。但是,包含 "filtered" 字段是什么意思(例如在上面评论的上下文中)?
那个comment
是由 go/printer
code based on the AST passed to it. Some of the AST nodes have a field that flags them as incomplete and this field is used by the printer to decide whether or not to print that comment. However the printer has no way of knowing the rules and reasons for why that field was set to true or false and so by convention it is assumend it was done by a filter, the most common one being exportFilter
产生的,因此是语言。
Incomplete
字段被导出并且可以被任何有权访问 AST 的东西设置为 true/false。您可以自己遍历 AST,将每个 Incomplete 字段设置为 true,同时保持节点完好无损,然后将 AST 传递给打印机,然后打印机将生成包含所有字段、已导出和未导出的结构,以及该注释。
Godoc filters the AST with ast.FileExports
which by default removes only unexported nodes, and then passes AST 到打印机。因此,在 Godoc 的情况下,该评论中的 "filtered" 与 "unexported".
Playground link 来说明打印机的行为。