Skip to content

Instantly share code, notes, and snippets.

@hoffoo
Created July 13, 2015 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoffoo/5283774e409ba7e8b003 to your computer and use it in GitHub Desktop.
Save hoffoo/5283774e409ba7e8b003 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
)
func main() {
file := "fail.go"
//path := "github.com/hoffoo/ffffar"
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, file, nil, parser.ParseComments|parser.AllErrors)
if err != nil {
panic(err)
}
for path, obj := range f.Scope.Objects {
fmt.Println(path)
switch d := obj.Decl.(type) {
case *ast.FuncType:
// XXX implement getting comments from these types
case *ast.StructType:
// XXX implement getting comments from these types
case *ast.FuncDecl:
if d.Doc == nil {
continue
}
if d.Doc.List != nil {
for _, comment := range d.Doc.List {
if comment != nil {
fmt.Printf("%s - %s\n", d.Name, comment.Text)
}
}
} else {
fmt.Printf("%s - %v", d.Doc.Text)
}
}
}
}
// This is a type
type T struct {
// this is a Member
Member int
}
// this is an instance fn
func (*T) instancemem() {
}
// this is a public fn
func pubmem() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment