github.com/yunabe/lgo@v0.0.0-20190709125917-42c42d410fdf/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  	src := `package foo
    17  
    18  import (
    19  	"fmt"
    20  	"time"
    21  )
    22  
    23  func bar() {
    24  	fmt.Println(time.Now())
    25  }`
    26  
    27  	// Parse src but stop after processing the imports.
    28  	f, err := parser.ParseFile(fset, "", src, parser.ImportsOnly)
    29  	if err != nil {
    30  		fmt.Println(err)
    31  		return
    32  	}
    33  
    34  	// Print the imports from the file's AST.
    35  	for _, s := range f.Imports {
    36  		fmt.Println(s.Path.Value)
    37  	}
    38  
    39  	// output:
    40  	//
    41  	// "fmt"
    42  	// "time"
    43  }