What is the difference between Doc and Comment in go/ast package?

huangapple go评论98阅读模式
英文:

What is the difference between Doc and Comment in go/ast package?

问题

我正在使用go/astgo/parser包来做一些事情,但我对DocComment之间的区别感到困惑。

第一行注释是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

src/go/ast/ast.go#L70-L75

// CommentGroup表示一系列没有其他标记和空行的注释。

根据Godoc:文档化Go代码

  • 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 the TypeSpec

> 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
    

huangapple
  • 本文由 发表于 2015年5月7日 13:11:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/30092417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定