github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/go/parser/example_test.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package parser_test
     6  
     7  import (
     8  	"fmt"
     9  	"go/parser"
    10  	"go/token"
    11  )
    12  
    13  func ExampleParseFile() {
    14  	fset := token.NewFileSet() // positions are relative to fset
    15  
    16  	// Parse the file containing this very example
    17  	// but stop after processing the imports.
    18  	f, err := parser.ParseFile(fset, "example_test.go", nil, parser.ImportsOnly)
    19  	if err != nil {
    20  		fmt.Println(err)
    21  		return
    22  	}
    23  
    24  	// Print the imports from the file's AST.
    25  	for _, s := range f.Imports {
    26  		fmt.Println(s.Path.Value)
    27  	}
    28  
    29  	// output:
    30  	//
    31  	// "fmt"
    32  	// "go/parser"
    33  	// "go/token"
    34  }