Why are Doc Comments for Struct Types Not Detected by the Go Parser?

Susan Sarandon
Release: 2024-11-07 04:39:02
Original
620 people have browsed it

Why are Doc Comments for Struct Types Not Detected by the Go Parser?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!