Skip to content

Instantly share code, notes, and snippets.

@awly
Created May 15, 2013 12:38
Show Gist options
  • Save awly/5583695 to your computer and use it in GitHub Desktop.
Save awly/5583695 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
)
type TestStruct struct {
s string
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Must supply a file name to process.")
return
}
fset := token.NewFileSet()
f, err := parser.ParseFile(
fset,
os.Args[1],
nil,
0,
)
if err != nil {
fmt.Println(err)
return
}
for _, v := range f.Scope.Objects {
if v.Kind == ast.Typ {
spec := v.Decl.(*ast.TypeSpec)
printStruct(spec)
fmt.Println()
}
}
}
func printStruct(spec *ast.TypeSpec) {
fmt.Println(spec.Name)
if struc, ok := spec.Type.(*ast.StructType); ok {
for _, field := range struc.Fields.List {
fmt.Println(field.Names, field.Type)
if _, ok := field.Type.(*ast.StructType); ok {
// get ast.Decl for this struct and recursively call printStruct
}
}
} else {
fmt.Println("not a struct")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment