package main import "fmt" import "go/parser" import "go/token" func p(x ...interface{}) { for _, e := range x { fmt.Printf("## %#v ", e) } fmt.Println() } func main() { fset := token.NewFileSet() // positions are relative to fset // Parse the file containing this very example // but stop after processing the imports. f, err := parser.ParseFile(fset, "a.go", nil, parser.Trace /*| parser.ParseComments */) if err != nil { fmt.Println(err) return } p("f", f) // Print the imports from the file's AST. for _, s := range f.Imports { fmt.Println(s.Path.Value) p(s) p(s.Path) p(s.Path.Value) } }