Go parser not detecting Doc comments on struct type
The question is about why the documentation comments for struct types are not being detected by the Go parser and ast packages. The code example provided uses the parser to iterate over the AST and print out the documentation for func, type, and field declarations. However, the documentation for the struct types is not being printed.
The answer explains that the documentation for struct types is attached to the GenDecl node in the AST, not to the TypeSpec node. This is because the AST sees the individual struct type definitions as "contractions" of the parenthesized version of type definitions. The go/doc package handles this case by generating a fake GenDecl node and passing it to the readType function if there is no documentation associated with the struct declaration.
The answer also explains why this is not the preferred method for parsing documentation comments, and recommends using the go/doc package instead.
The above is the detailed content of Why are Doc Comments for Struct Types Not Detected by the Go Parser?. For more information, please follow other related articles on the PHP Chinese website!