英文:
What is the difference between Doc and Comment in go/ast package?
问题
我正在使用go/ast
和go/parser
包来做一些事情,但我对Doc
和Comment
之间的区别感到困惑。
第一行注释是Doc
,其他行是Comment
吗?
这是一个示例:
TypeSpec struct {
Doc *CommentGroup // 相关文档; 或者为nil
Name *Ident // 类型名称
Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, 或者任何 *XxxTypes
Comment *CommentGroup // 行注释; 或者为nil
}
英文:
I am using the go/ast
and go/parser
package to do something, but i am puzzled about the difference between Doc
and Comment
.
Is the first line of comments a Doc
, then others as Comment
?
Here is a sample:
TypeSpec struct {
Doc *CommentGroup // associated documentation; or nil
Name *Ident // type name
Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
Comment *CommentGroup // line comments; or nil
}
答案1
得分: 2
// CommentGroup表示一系列没有其他标记和空行的注释。
Doc
是一个或多个连续的注释行(// ...
),位于TypeSpec
之前
在其声明之前直接编写一个常规注释,没有空行
// TypeSpec节点表示类型声明(TypeSpec production)。
^^^^^^^^^^^^...
TypeSpec struct {
-
Comment
是与字段本身关联的注释,从同一行开始,但可以跨多行(因此称为“CommentGroup
”)Name *Ident // 类型名称 ^^^^^^^^^^^ // 与Name相关联的注释 // 可以跨多行
英文:
From src/go/ast/ast.go#L70-L75
:
// A CommentGroup represents a sequence of comments
// with no other tokens and no empty lines between.
Following Godoc: documenting Go code:
Doc
is one or several continuous lines of comments (// ...
) before theTypeSpec
> write a regular comment directly preceding its declaration, with no intervening blank line
// A TypeSpec node represents a type declaration (TypeSpec production).
^^^^^^^^^^^^...
TypeSpec struct {
-
Comment
is a comment associate to the field itself, starting on the same line, but which can spread over multiple continuous lines (hence "CommentGroup
")Name *Ident // type name ^^^^^^^^^^^ // the comment associated to Name // could go on over several lines
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论