Q: Accessing to a comment within a function in Go I'm currently working on documentation generator that will parse Go code to produce a custom documentation. I need to access to the comments written inside a function. But I can't retrieve these comments in the AST, neither with the go/doc. Here is an example :
package main

import (
    "fmt"
    "go/doc"
    "go/parser"
    "go/token"
)

// GetFoo comments I can find easely
func GetFoo() {
    // Comment I would like to access
    test := 1
    fmt.Println(test)
}

func main() {
    fset := token.NewFileSet() // positions are relative to fset
    d, err := parser.ParseDir(fset, "./", nil, parser.ParseComments)
    if err != nil {
        fmt.Println(err)
        return
    }
    for k, f := range d {
        fmt.Println("package", k)
        p := doc.New(f, "./", 2)

        for _, t := range p.Types {
            fmt.Println("type", t.Name)
            fmt.Println("docs:", t.Doc)
        }

        for _, v := range p.Vars {
            fmt.Println("type", v.Names)
            fmt.Println("docs:", v.Doc)
        }

        for _, f := range p.Funcs {
            fmt.Println("type", f.Name)
            fmt.Println("docs:", f.Doc)
        }

        for _, n := range p.Notes {
            fmt.Println("body", n[0].Body)
        }
    }
}

It's easy to find GetFoo comments I can find easely but not Comment I would like to access
I have seen this post quite similar question Go parser not detecting Doc comments on struct type but for exported types
Is there any way to do that ? Thank you !

A: The problem is that the doc.New functionality is only parsing for documentation strings, and the comment inside the function is not part of the "documentation".
You'll want to directly iterate the ast of the files in the package.
package main

import (
        "fmt"
        "go/parser"
        "go/token"
)

// GetFoo comments I can find easely
func GetFoo() {
        // Comment I would like to access
        test := 1
        fmt.Println(test)
}

func main() {
        fset := token.NewFileSet() // positions are relative to fset
        d, err := parser.ParseDir(fset, "./", nil, parser.ParseComments)
        if err != nil {
                fmt.Println(err)
                return
        }
        for k, f := range d {
                fmt.Println("package", k)
                for n, f := range f.Files {
                        fmt.Printf("File name: %q\n", n)
                        for i, c := range f.Comments {
                                fmt.Printf("Comment Group %d\n", i)
                                for i2, c1 := range c.List {
                                        fmt.Printf("Comment %d: Position: %d, Text: %q\n", i2, c1.Slash, c1.Text)
                                }
                        }
                }

        }
}

