Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created November 29, 2015 15:49
Show Gist options
  • Save iporsut/0db5ac9e110e9d2185f4 to your computer and use it in GitHub Desktop.
Save iporsut/0db5ac9e110e9d2185f4 to your computer and use it in GitHub Desktop.
List variable decralation in package
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"strings"
)
func main() {
fset := token.NewFileSet() // positions are relative to fset
pkgs, _ := parser.ParseDir(fset, os.Args[1], nil, 0)
for pname, p := range pkgs {
if !strings.HasSuffix(pname, "_test") {
for _, f := range p.Files {
for _, d := range f.Decls {
switch gd := d.(type) {
case *ast.GenDecl:
if gd.Tok == token.VAR {
for _, s := range gd.Specs {
fmt.Println(s.(*ast.ValueSpec).Names[0].Name)
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment