Why is My Go Parser Not Detecting Doc Comments on Struct Types?

Mary-Kate Olsen
Release: 2024-11-07 19:32:02
Original
978 people have browsed it

Why is My Go Parser Not Detecting Doc Comments on Struct Types?

Go Parser Not Detecting Doc Comments on Struct Type

In Go, identifying the associated Doc comments for a struct type can be achieved through the parser and ast packages. Here's an investigation into why these comments may not be detected:

The Issue

Source code provided in the question demonstrates an attempt to retrieve Doc comments for a struct type using Go's parser package. However, the Doc comments for the struct types FirstType and SecondType are missing in the output.

The Solution

To resolve this, we need to consider how Go handles documentation in its abstract syntax tree (AST). The go/doc package suggests that when there is no documentation attached to a TypeSpec, the documentation of the parent GenDecl should be used instead.

Inspecting GenDecls

Adapting the code from the question to include the case for *ast.GenDecl:

for _, f := range d {
    ast.Inspect(f, func(n ast.Node) bool {
        switch x := n.(type) {
        case *ast.FuncDecl:
            fmt.Printf("%s:\tFuncDecl %s\t%s\n", fset.Position(n.Pos()), x.Name, x.Doc)
        case *ast.TypeSpec:
            fmt.Printf("%s:\tTypeSpec %s\t%s\n", fset.Position(n.Pos()), x.Name, x.Doc)
        case *ast.Field:
            fmt.Printf("%s:\tField %s\t%s\n", fset.Position(n.Pos()), x.Names, x.Doc)
        case *ast.GenDecl:
            fmt.Printf("%s:\tGenDecl %s\n", fset.Position(n.Pos()), x.Doc)
        }

        return true
    })
}
Copy after login

Running this code yields the missing Doc comments for FirstType and SecondType.

Unexpected Behavior

While this method uncovers the missing Doc comments, it also reveals that the documentation for the GenDecl is unintentionally included. This is because the AST sees struct type definitions as contractions of the parenthesized version of type definitions, handling all definitions similarly regardless of grouping.

Conclusion

To effectively retrieve Doc comments for struct types using pure AST, it is crucial to acknowledge this behavior. However, employing the go/doc package remains the preferred approach as it circumvents these complexities and provides a comprehensive solution for documentation parsing in Go.

The above is the detailed content of Why is My Go Parser Not Detecting Doc Comments on Struct Types?. 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!