Created
May 15, 2013 12:38
-
-
Save awly/5583695 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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